Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit db3cb25

Browse files
author
Openset
committed
Update: math
1 parent 44edfc5 commit db3cb25

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

‎solution/string-to-integer-atoi/string_to_integer_atoi.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package string_to_integer_atoi
22

33
import (
4+
"math"
5+
"strconv"
46
"strings"
57
"unicode"
6-
"strconv"
78
)
89

910
func myAtoi(str string) int {
@@ -20,10 +21,10 @@ func myAtoi(str string) int {
2021
str = str[0:i]
2122
}
2223
r, _ := strconv.Atoi(prefix + str)
23-
if r < -1<<31 {
24-
r = -1<<31
25-
} else if r > 1<<31-1 {
26-
r = 1<<31-1
24+
if r < math.MinInt32 {
25+
r = math.MinInt32
26+
} else if r > math.MaxInt32 {
27+
r = math.MaxInt32
2728
}
2829
return r
2930
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /