File tree 5 files changed +23
-4
lines changed
solution/0000-0099/0008.String to Integer (atoi)
5 files changed +23
-4
lines changed Original file line number Diff line number Diff line change 68
68
69
69
遍历字符串,注意做溢出处理。
70
70
71
+ 同[ 字符串转换整数 (atoi)] ( /solution/0000-0099/0008.String%20to%20Integer%20%28atoi%29/README.md ) 。
72
+
71
73
<!-- tabs:start -->
72
74
73
75
### ** Python3**
@@ -85,16 +87,19 @@ class Solution:
85
87
i = 0
86
88
while str[i] == ' ' :
87
89
i += 1
90
+ # 仅包含空格
88
91
if i == n:
89
92
return 0
90
93
sign = - 1 if str[i] == ' -' else 1
91
94
if str[i] in [' -' , ' +' ]:
92
95
i += 1
93
96
res, flag = 0 , (2 ** 31 - 1 ) // 10
94
97
while i < n:
98
+ # 非数字,跳出循环体
95
99
if not str[i].isdigit():
96
100
break
97
101
c = int (str[i])
102
+ # 溢出判断
98
103
if res > flag or (res == flag and c > 7 ):
99
104
return 2 ** 31 - 1 if sign > 0 else - 2 ** 31
100
105
res = res * 10 + c
@@ -122,7 +127,7 @@ class Solution {
122
127
if (str. charAt(i) == ' -' || str. charAt(i) == ' +' ) ++ i;
123
128
int res = 0 , flag = Integer . MAX_VALUE / 10 ;
124
129
for (; i < n; ++ i) {
125
- // 非数字
130
+ // 非数字,跳出循环体
126
131
if (str. charAt(i) < ' 0' || str. charAt(i) > ' 9' ) break ;
127
132
// 溢出判断
128
133
if (res > flag || (res == flag) && str. charAt(i) > ' 7' ) return sign > 0 ? Integer . MAX_VALUE : Integer . MIN_VALUE ;
Original file line number Diff line number Diff line change @@ -8,16 +8,19 @@ def strToInt(self, str: str) -> int:
8
8
i = 0
9
9
while str [i ] == ' ' :
10
10
i += 1
11
+ # 仅包含空格
11
12
if i == n :
12
13
return 0
13
14
sign = - 1 if str [i ] == '-' else 1
14
15
if str [i ] in ['-' , '+' ]:
15
16
i += 1
16
17
res , flag = 0 , (2 ** 31 - 1 ) // 10
17
18
while i < n :
19
+ # 非数字,跳出循环
18
20
if not str [i ].isdigit ():
19
21
break
20
22
c = int (str [i ])
23
+ # 溢出判断
21
24
if res > flag or (res == flag and c > 7 ):
22
25
return 2 ** 31 - 1 if sign > 0 else - 2 ** 31
23
26
res = res * 10 + c
Original file line number Diff line number Diff line change 61
61
62
62
<!-- 这里可写通用的实现逻辑 -->
63
63
64
- 同[ 面试题 67. 把字符串转换成整数] ( /lcof/面试题67.%20把字符串转换成整数/README.md )
64
+ 遍历字符串,注意做溢出处理。
65
+
66
+ 同[ 面试题 67. 把字符串转换成整数] ( /lcof/面试题67.%20把字符串转换成整数/README.md ) 。
65
67
66
68
<!-- tabs:start -->
67
69
@@ -80,16 +82,19 @@ class Solution:
80
82
i = 0
81
83
while s[i] == ' ' :
82
84
i += 1
85
+ # 仅包含空格
83
86
if i == n:
84
87
return 0
85
88
sign = - 1 if s[i] == ' -' else 1
86
89
if s[i] in [' -' , ' +' ]:
87
90
i += 1
88
91
res, flag = 0 , (2 ** 31 - 1 ) // 10
89
92
while i < n:
93
+ # 非数字,跳出循环体
90
94
if not s[i].isdigit():
91
95
break
92
96
c = int (s[i])
97
+ # 溢出判断
93
98
if res > flag or (res == flag and c > 7 ):
94
99
return 2 ** 31 - 1 if sign > 0 else - 2 ** 31
95
100
res = res * 10 + c
@@ -117,7 +122,7 @@ class Solution {
117
122
if (s. charAt(i) == ' -' || s. charAt(i) == ' +' ) ++ i;
118
123
int res = 0 , flag = Integer . MAX_VALUE / 10 ;
119
124
for (; i < n; ++ i) {
120
- // 非数字
125
+ // 非数字,跳出循环体
121
126
if (s. charAt(i) < ' 0' || s. charAt(i) > ' 9' ) break ;
122
127
// 溢出判断
123
128
if (res > flag || (res == flag && s. charAt(i) > ' 7' )) return sign > 0 ? Integer . MAX_VALUE : Integer . MIN_VALUE ;
Original file line number Diff line number Diff line change @@ -98,16 +98,19 @@ class Solution:
98
98
i = 0
99
99
while s[i] == ' ' :
100
100
i += 1
101
+ # only contains blank space
101
102
if i == n:
102
103
return 0
103
104
sign = - 1 if s[i] == ' -' else 1
104
105
if s[i] in [' -' , ' +' ]:
105
106
i += 1
106
107
res, flag = 0 , (2 ** 31 - 1 ) // 10
107
108
while i < n:
109
+ # not a number, exit the loop
108
110
if not s[i].isdigit():
109
111
break
110
112
c = int (s[i])
113
+ # if overflows
111
114
if res > flag or (res == flag and c > 7 ):
112
115
return 2 ** 31 - 1 if sign > 0 else - 2 ** 31
113
116
res = res * 10 + c
@@ -133,7 +136,7 @@ class Solution {
133
136
if (s. charAt(i) == ' -' || s. charAt(i) == ' +' ) ++ i;
134
137
int res = 0 , flag = Integer . MAX_VALUE / 10 ;
135
138
for (; i < n; ++ i) {
136
- // not number
139
+ // not a number, exit the loop
137
140
if (s. charAt(i) < ' 0' || s. charAt(i) > ' 9' ) break ;
138
141
// if overflows
139
142
if (res > flag || (res == flag && s. charAt(i) > ' 7' )) return sign > 0 ? Integer . MAX_VALUE : Integer . MIN_VALUE ;
Original file line number Diff line number Diff line change @@ -8,16 +8,19 @@ def myAtoi(self, s: str) -> int:
8
8
i = 0
9
9
while s [i ] == ' ' :
10
10
i += 1
11
+ # only contains blank space
11
12
if i == n :
12
13
return 0
13
14
sign = - 1 if s [i ] == '-' else 1
14
15
if s [i ] in ['-' , '+' ]:
15
16
i += 1
16
17
res , flag = 0 , (2 ** 31 - 1 ) // 10
17
18
while i < n :
19
+ # not a number, exit the loop
18
20
if not s [i ].isdigit ():
19
21
break
20
22
c = int (s [i ])
23
+ # if overflows
21
24
if res > flag or (res == flag and c > 7 ):
22
25
return 2 ** 31 - 1 if sign > 0 else - 2 ** 31
23
26
res = res * 10 + c
You can’t perform that action at this time.
0 commit comments