Skip to content

Commit e1b5abf

Browse files
committed
fix typo
1 parent 78497cf commit e1b5abf

File tree

1 file changed

+5
-4
lines changed
  • pullrequests/string_to_integer_atoi

1 file changed

+5
-4
lines changed

pullrequests/string_to_integer_atoi/step3.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func myAtoi(s string) int {
1717
i++
1818
}
1919

20-
sign := 0
20+
sign := 1
2121
if i < len(s) && (s[i] == '-' || s[i] == '+') {
2222
if s[i] == '-' {
2323
sign = -1
@@ -27,13 +27,14 @@ func myAtoi(s string) int {
2727

2828
num := 0
2929
for i < len(s) && '0' <= s[i] && s[i] <= '9' {
30-
num = num*10 + int(s[i]-'0')
31-
if sign == 1 && num > intMax {
30+
next := int(s[i] - '0')
31+
if sign != -1 && num > (intMax-next)/10 {
3232
return intMax
3333
}
34-
if sign == -1 && -num < intMin {
34+
if sign == -1 && -num < (intMin+next)/10 {
3535
return intMin
3636
}
37+
num = num*10 + next
3738
i++
3839
}
3940
return sign * num

0 commit comments

Comments
 (0)