Skip to content

Commit c9cdb8b

Browse files
committed
feat: update solutions to leetcode problem
1 parent d57908a commit c9cdb8b

File tree

5 files changed

+23
-4
lines changed

5 files changed

+23
-4
lines changed

lcof/面试题67. 把字符串转换成整数/README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@
6868

6969
遍历字符串,注意做溢出处理。
7070

71+
[字符串转换整数 (atoi)](/solution/0000-0099/0008.String%20to%20Integer%20%28atoi%29/README.md)
72+
7173
<!-- tabs:start -->
7274

7375
### **Python3**
@@ -85,16 +87,19 @@ class Solution:
8587
i = 0
8688
while str[i] == ' ':
8789
i += 1
90+
# 仅包含空格
8891
if i == n:
8992
return 0
9093
sign = -1 if str[i] == '-' else 1
9194
if str[i] in ['-', '+']:
9295
i += 1
9396
res, flag = 0, (2 ** 31 - 1) // 10
9497
while i < n:
98+
# 非数字,跳出循环体
9599
if not str[i].isdigit():
96100
break
97101
c = int(str[i])
102+
# 溢出判断
98103
if res > flag or (res == flag and c > 7):
99104
return 2 ** 31 - 1 if sign > 0 else -2 ** 31
100105
res = res * 10 + c
@@ -122,7 +127,7 @@ class Solution {
122127
if (str.charAt(i) == '-' || str.charAt(i) == '+') ++i;
123128
int res = 0, flag = Integer.MAX_VALUE / 10;
124129
for (; i < n; ++i) {
125-
// 非数字
130+
// 非数字,跳出循环体
126131
if (str.charAt(i) < '0' || str.charAt(i) > '9') break;
127132
// 溢出判断
128133
if (res > flag || (res == flag) && str.charAt(i) > '7') return sign > 0 ? Integer.MAX_VALUE : Integer.MIN_VALUE;

lcof/面试题67. 把字符串转换成整数/Solution.py

+3
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,19 @@ def strToInt(self, str: str) -> int:
88
i = 0
99
while str[i] == ' ':
1010
i += 1
11+
# 仅包含空格
1112
if i == n:
1213
return 0
1314
sign = -1 if str[i] == '-' else 1
1415
if str[i] in ['-', '+']:
1516
i += 1
1617
res, flag = 0, (2 ** 31 - 1) // 10
1718
while i < n:
19+
# 非数字,跳出循环
1820
if not str[i].isdigit():
1921
break
2022
c = int(str[i])
23+
# 溢出判断
2124
if res > flag or (res == flag and c > 7):
2225
return 2 ** 31 - 1 if sign > 0 else -2 ** 31
2326
res = res * 10 + c

solution/0000-0099/0008.String to Integer (atoi)/README.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@
6161

6262
<!-- 这里可写通用的实现逻辑 -->
6363

64-
[面试题 67. 把字符串转换成整数](/lcof/面试题67.%20把字符串转换成整数/README.md)
64+
遍历字符串,注意做溢出处理。
65+
66+
[面试题 67. 把字符串转换成整数](/lcof/面试题67.%20把字符串转换成整数/README.md)
6567

6668
<!-- tabs:start -->
6769

@@ -80,16 +82,19 @@ class Solution:
8082
i = 0
8183
while s[i] == ' ':
8284
i += 1
85+
# 仅包含空格
8386
if i == n:
8487
return 0
8588
sign = -1 if s[i] == '-' else 1
8689
if s[i] in ['-', '+']:
8790
i += 1
8891
res, flag = 0, (2 ** 31 - 1) // 10
8992
while i < n:
93+
# 非数字,跳出循环体
9094
if not s[i].isdigit():
9195
break
9296
c = int(s[i])
97+
# 溢出判断
9398
if res > flag or (res == flag and c > 7):
9499
return 2 ** 31 - 1 if sign > 0 else -2 ** 31
95100
res = res * 10 + c
@@ -117,7 +122,7 @@ class Solution {
117122
if (s.charAt(i) == '-' || s.charAt(i) == '+') ++i;
118123
int res = 0, flag = Integer.MAX_VALUE / 10;
119124
for (; i < n; ++i) {
120-
// 非数字
125+
// 非数字,跳出循环体
121126
if (s.charAt(i) < '0' || s.charAt(i) > '9') break;
122127
// 溢出判断
123128
if (res > flag || (res == flag && s.charAt(i) > '7')) return sign > 0 ? Integer.MAX_VALUE : Integer.MIN_VALUE;

solution/0000-0099/0008.String to Integer (atoi)/README_EN.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,19 @@ class Solution:
9898
i = 0
9999
while s[i] == ' ':
100100
i += 1
101+
# only contains blank space
101102
if i == n:
102103
return 0
103104
sign = -1 if s[i] == '-' else 1
104105
if s[i] in ['-', '+']:
105106
i += 1
106107
res, flag = 0, (2 ** 31 - 1) // 10
107108
while i < n:
109+
# not a number, exit the loop
108110
if not s[i].isdigit():
109111
break
110112
c = int(s[i])
113+
# if overflows
111114
if res > flag or (res == flag and c > 7):
112115
return 2 ** 31 - 1 if sign > 0 else -2 ** 31
113116
res = res * 10 + c
@@ -133,7 +136,7 @@ class Solution {
133136
if (s.charAt(i) == '-' || s.charAt(i) == '+') ++i;
134137
int res = 0, flag = Integer.MAX_VALUE / 10;
135138
for (; i < n; ++i) {
136-
// not number
139+
// not a number, exit the loop
137140
if (s.charAt(i) < '0' || s.charAt(i) > '9') break;
138141
// if overflows
139142
if (res > flag || (res == flag && s.charAt(i) > '7')) return sign > 0 ? Integer.MAX_VALUE : Integer.MIN_VALUE;

solution/0000-0099/0008.String to Integer (atoi)/Solution.py

+3
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,19 @@ def myAtoi(self, s: str) -> int:
88
i = 0
99
while s[i] == ' ':
1010
i += 1
11+
# only contains blank space
1112
if i == n:
1213
return 0
1314
sign = -1 if s[i] == '-' else 1
1415
if s[i] in ['-', '+']:
1516
i += 1
1617
res, flag = 0, (2 ** 31 - 1) // 10
1718
while i < n:
19+
# not a number, exit the loop
1820
if not s[i].isdigit():
1921
break
2022
c = int(s[i])
23+
# if overflows
2124
if res > flag or (res == flag and c > 7):
2225
return 2 ** 31 - 1 if sign > 0 else -2 ** 31
2326
res = res * 10 + c

0 commit comments

Comments
 (0)