We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 78497cf commit e1b5abfCopy full SHA for e1b5abf
pullrequests/string_to_integer_atoi/step3.go
@@ -17,7 +17,7 @@ func myAtoi(s string) int {
17
i++
18
}
19
20
- sign := 0
+ sign := 1
21
if i < len(s) && (s[i] == '-' || s[i] == '+') {
22
if s[i] == '-' {
23
sign = -1
@@ -27,13 +27,14 @@ func myAtoi(s string) int {
27
28
num := 0
29
for i < len(s) && '0' <= s[i] && s[i] <= '9' {
30
- num = num*10 + int(s[i]-'0')
31
- if sign == 1 && num > intMax {
+ next := int(s[i] - '0')
+ if sign != -1 && num > (intMax-next)/10 {
32
return intMax
33
34
- if sign == -1 && -num < intMin {
+ if sign == -1 && -num < (intMin+next)/10 {
35
return intMin
36
37
+ num = num*10 + next
38
39
40
return sign * num
0 commit comments