File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed
pullrequests/string_to_integer_atoi Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change
1
+ //lint:file-ignore U1000 Ignore all unused code
2
+ package stringtointegeratoi
3
+
4
+ import "math"
5
+
6
+ /*
7
+ Step3のオーバーフローしてしまうバグを直しました。
8
+ */
9
+ func myAtoi_step4 (s string ) int {
10
+ const (
11
+ intMax = int (math .MaxInt32 )
12
+ intMin = int (math .MinInt32 )
13
+ )
14
+
15
+ i := 0
16
+ for i < len (s ) && s [i ] == ' ' {
17
+ i ++
18
+ }
19
+
20
+ sign := 1
21
+ if i < len (s ) && (s [i ] == '-' || s [i ] == '+' ) {
22
+ if s [i ] == '-' {
23
+ sign = - 1
24
+ }
25
+ i ++
26
+ }
27
+
28
+ num := 0
29
+ for i < len (s ) && '0' <= s [i ] && s [i ] <= '9' {
30
+ digit := int (s [i ] - '0' )
31
+ if sign == 1 && (num > intMax / 10 || num == intMax / 10 && digit >= intMax % 10 ) {
32
+ return intMax
33
+ }
34
+ if sign == - 1 && (- num < intMin / 10 || - num == intMin / 10 && - digit <= intMin % 10 ) {
35
+ return intMin
36
+ }
37
+ num = num * 10 + digit
38
+ i ++
39
+ }
40
+ return sign * num
41
+ }
You can’t perform that action at this time.
0 commit comments