Skip to content

Commit d57908a

Browse files
committed
feat: update solutions to leetcode and lcof problems: atoi
1 parent e6ff50b commit d57908a

File tree

9 files changed

+215
-115
lines changed

9 files changed

+215
-115
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
### 字符串
6464

6565
1. [反转字符串中的元音字母](/solution/0300-0399/0345.Reverse%20Vowels%20of%20a%20String/README.md)
66+
1. [字符串转换整数 (atoi)](/solution/0000-0099/0008.String%20to%20Integer%20%28atoi%29/README.md)
6667

6768
### 链表
6869

README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
6262
### Strings
6363

6464
1. [Reverse Vowels of a String](/solution/0300-0399/0345.Reverse%20Vowels%20of%20a%20String/README_EN.md)
65+
1. [String to Integer (atoi)](/solution/0000-0099/0008.String%20to%20Integer%20%28atoi%29/README_EN.md)
6566

6667
### Linked List
6768

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

+38-31
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,29 @@
7777
```python
7878
class Solution:
7979
def strToInt(self, str: str) -> int:
80-
if str is None or len(str.strip()) == 0:
80+
if not str:
8181
return 0
82-
str = str.strip()
83-
i = res = 0
84-
negative = str[0] == '-'
85-
i += 1 if str[0] == '-' or str[0] == '+' else 0
86-
while i < len(str) and str[i].isdigit():
87-
r = int(str[i])
88-
if res > (2**31 // 10) or (res == (2**31 // 10) and r > 7):
89-
return 2**31 - 1 if not negative else -2**31
90-
res = res * 10 + r
82+
n = len(str)
83+
if n == 0:
84+
return 0
85+
i = 0
86+
while str[i] == ' ':
87+
i += 1
88+
if i == n:
89+
return 0
90+
sign = -1 if str[i] == '-' else 1
91+
if str[i] in ['-', '+']:
92+
i += 1
93+
res, flag = 0, (2 ** 31 - 1) // 10
94+
while i < n:
95+
if not str[i].isdigit():
96+
break
97+
c = int(str[i])
98+
if res > flag or (res == flag and c > 7):
99+
return 2 ** 31 - 1 if sign > 0 else -2 ** 31
100+
res = res * 10 + c
91101
i += 1
92-
return -res if negative else res
102+
return sign * res
93103
```
94104

95105
### **Java**
@@ -99,29 +109,26 @@ class Solution:
99109
```java
100110
class Solution {
101111
public int strToInt(String str) {
102-
if (str == null || "".equals(str.trim())) {
103-
return 0;
104-
}
105-
str = str.trim();
106-
int res = 0, i = 0, flag = 1;
112+
if (str == null) return 0;
107113
int n = str.length();
108-
if (str.charAt(i) == '-') {
109-
flag = -1;
110-
}
111-
if (str.charAt(i) == '-' || str.charAt(i) == '+') {
112-
++i;
114+
if (n == 0) return 0;
115+
int i = 0;
116+
while (str.charAt(i) == ' ') {
117+
// 仅包含空格
118+
if (++i == n) return 0;
113119
}
114-
while (i < n && Character.isDigit(str.charAt(i))) {
115-
int r = str.charAt(i) - '0';
116-
// 溢出处理
117-
if (res > Integer.MAX_VALUE / 10 || (res == Integer.MAX_VALUE / 10) && r > 7) {
118-
return flag > 0 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
119-
}
120-
res = res * 10 + r;
121-
++i;
120+
int sign = 1;
121+
if (str.charAt(i) == '-') sign = -1;
122+
if (str.charAt(i) == '-' || str.charAt(i) == '+') ++i;
123+
int res = 0, flag = Integer.MAX_VALUE / 10;
124+
for (; i < n; ++i) {
125+
// 非数字
126+
if (str.charAt(i) < '0' || str.charAt(i) > '9') break;
127+
// 溢出判断
128+
if (res > flag || (res == flag) && str.charAt(i) > '7') return sign > 0 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
129+
res = res * 10 + (str.charAt(i) - '0');
122130
}
123-
124-
return flag > 0 ? res : -res;
131+
return sign * res;
125132
}
126133
}
127134
```
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,21 @@
11
class Solution {
22
public int strToInt(String str) {
3-
if (str == null || "".equals(str.trim())) {
4-
return 0;
5-
}
6-
str = str.trim();
7-
int res = 0, i = 0, flag = 1;
3+
if (str == null) return 0;
84
int n = str.length();
9-
if (str.charAt(i) == '-') {
10-
flag = -1;
11-
}
12-
if (str.charAt(i) == '-' || str.charAt(i) == '+') {
13-
++i;
5+
if (n == 0) return 0;
6+
int i = 0;
7+
while (str.charAt(i) == ' ') {
8+
if (++i == n) return 0;
149
}
15-
while (i < n && Character.isDigit(str.charAt(i))) {
16-
int r = str.charAt(i) - '0';
17-
// 溢出处理
18-
if (res > Integer.MAX_VALUE / 10 || (res == Integer.MAX_VALUE / 10) && r > 7) {
19-
return flag > 0 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
20-
}
21-
res = res * 10 + r;
22-
++i;
10+
int sign = 1;
11+
if (str.charAt(i) == '-') sign = -1;
12+
if (str.charAt(i) == '-' || str.charAt(i) == '+') ++i;
13+
int res = 0, flag = Integer.MAX_VALUE / 10;
14+
for (; i < n; ++i) {
15+
if (str.charAt(i) < '0' || str.charAt(i) > '9') break;
16+
if (res > flag || (res == flag) && str.charAt(i) > '7') return sign > 0 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
17+
res = res * 10 + (str.charAt(i) - '0');
2318
}
24-
25-
return flag > 0 ? res : -res;
19+
return sign * res;
2620
}
2721
}
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
class Solution:
22
def strToInt(self, str: str) -> int:
3-
if str is None or len(str.strip()) == 0:
3+
if not str:
44
return 0
5-
str = str.strip()
6-
i = res = 0
7-
negative = str[0] == '-'
8-
i += 1 if str[0] == '-' or str[0] == '+' else 0
9-
while i < len(str) and str[i].isdigit():
10-
r = int(str[i])
11-
if res > (2**31 // 10) or (res == (2**31 // 10) and r > 7):
12-
return 2**31 - 1 if not negative else -2**31
13-
res = res * 10 + r
5+
n = len(str)
6+
if n == 0:
7+
return 0
8+
i = 0
9+
while str[i] == ' ':
10+
i += 1
11+
if i == n:
12+
return 0
13+
sign = -1 if str[i] == '-' else 1
14+
if str[i] in ['-', '+']:
15+
i += 1
16+
res, flag = 0, (2 ** 31 - 1) // 10
17+
while i < n:
18+
if not str[i].isdigit():
19+
break
20+
c = int(str[i])
21+
if res > flag or (res == flag and c > 7):
22+
return 2 ** 31 - 1 if sign > 0 else -2 ** 31
23+
res = res * 10 + c
1424
i += 1
15-
return -res if negative else res
25+
return sign * res

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

+51-2
Original file line numberDiff line numberDiff line change
@@ -61,22 +61,71 @@
6161

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

64+
[面试题 67. 把字符串转换成整数](/lcof/面试题67.%20把字符串转换成整数/README.md)
65+
6466
<!-- tabs:start -->
6567

6668
### **Python3**
6769

6870
<!-- 这里可写当前语言的特殊实现逻辑 -->
6971

7072
```python
71-
73+
class Solution:
74+
def myAtoi(self, s: str) -> int:
75+
if not s:
76+
return 0
77+
n = len(s)
78+
if n == 0:
79+
return 0
80+
i = 0
81+
while s[i] == ' ':
82+
i += 1
83+
if i == n:
84+
return 0
85+
sign = -1 if s[i] == '-' else 1
86+
if s[i] in ['-', '+']:
87+
i += 1
88+
res, flag = 0, (2 ** 31 - 1) // 10
89+
while i < n:
90+
if not s[i].isdigit():
91+
break
92+
c = int(s[i])
93+
if res > flag or (res == flag and c > 7):
94+
return 2 ** 31 - 1 if sign > 0 else -2 ** 31
95+
res = res * 10 + c
96+
i += 1
97+
return sign * res
7298
```
7399

74100
### **Java**
75101

76102
<!-- 这里可写当前语言的特殊实现逻辑 -->
77103

78104
```java
79-
105+
class Solution {
106+
public int myAtoi(String s) {
107+
if (s == null) return 0;
108+
int n = s.length();
109+
if (n == 0) return 0;
110+
int i = 0;
111+
while (s.charAt(i) == ' ') {
112+
// 仅包含空格
113+
if (++i == n) return 0;
114+
}
115+
int sign = 1;
116+
if (s.charAt(i) == '-') sign = -1;
117+
if (s.charAt(i) == '-' || s.charAt(i) == '+') ++i;
118+
int res = 0, flag = Integer.MAX_VALUE / 10;
119+
for (; i < n; ++i) {
120+
// 非数字
121+
if (s.charAt(i) < '0' || s.charAt(i) > '9') break;
122+
// 溢出判断
123+
if (res > flag || (res == flag && s.charAt(i) > '7')) return sign > 0 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
124+
res = res * 10 + (s.charAt(i) - '0');
125+
}
126+
return sign * res;
127+
}
128+
}
80129
```
81130

82131
### **...**

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

+49-2
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,60 @@
8888
### **Python3**
8989

9090
```python
91-
91+
class Solution:
92+
def myAtoi(self, s: str) -> int:
93+
if not s:
94+
return 0
95+
n = len(s)
96+
if n == 0:
97+
return 0
98+
i = 0
99+
while s[i] == ' ':
100+
i += 1
101+
if i == n:
102+
return 0
103+
sign = -1 if s[i] == '-' else 1
104+
if s[i] in ['-', '+']:
105+
i += 1
106+
res, flag = 0, (2 ** 31 - 1) // 10
107+
while i < n:
108+
if not s[i].isdigit():
109+
break
110+
c = int(s[i])
111+
if res > flag or (res == flag and c > 7):
112+
return 2 ** 31 - 1 if sign > 0 else -2 ** 31
113+
res = res * 10 + c
114+
i += 1
115+
return sign * res
92116
```
93117

94118
### **Java**
95119

96120
```java
97-
121+
class Solution {
122+
public int myAtoi(String s) {
123+
if (s == null) return 0;
124+
int n = s.length();
125+
if (n == 0) return 0;
126+
int i = 0;
127+
while (s.charAt(i) == ' ') {
128+
// only contains blank space
129+
if (++i == n) return 0;
130+
}
131+
int sign = 1;
132+
if (s.charAt(i) == '-') sign = -1;
133+
if (s.charAt(i) == '-' || s.charAt(i) == '+') ++i;
134+
int res = 0, flag = Integer.MAX_VALUE / 10;
135+
for (; i < n; ++i) {
136+
// not number
137+
if (s.charAt(i) < '0' || s.charAt(i) > '9') break;
138+
// if overflows
139+
if (res > flag || (res == flag && s.charAt(i) > '7')) return sign > 0 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
140+
res = res * 10 + (s.charAt(i) - '0');
141+
}
142+
return sign * res;
143+
}
144+
}
98145
```
99146

100147
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,21 @@
11
class Solution {
2-
public int myAtoi(String str) {
3-
int len = str.length();
4-
if (len == 0) return 0;
5-
char[] cs = str.toCharArray();
2+
public int myAtoi(String s) {
3+
if (s == null) return 0;
4+
int n = s.length();
5+
if (n == 0) return 0;
66
int i = 0;
7-
while (i < len && cs[i] == ' ') i++;
8-
if (i==len) return 0;
9-
char c1 = cs[i];
10-
int sig = 1;
11-
if ((c1 > '9' || c1 < '0')) {
12-
if (c1 == '-') {
13-
sig = -1;
14-
i++;
15-
} else if (c1 == '+') {
16-
i++;
17-
} else return 0;
7+
while (s.charAt(i) == ' ') {
8+
if (++i == n) return 0;
189
}
19-
long v = 0,sv = 0;
20-
for (; i < len; i++) {
21-
char c = cs[i];
22-
if (c < '0' || c > '9') break;
23-
v = v * 10 + (c - '0');
24-
sv = v * sig;
25-
if (sv > Integer.MAX_VALUE) return Integer.MAX_VALUE;
26-
else if (sv < Integer.MIN_VALUE) return Integer.MIN_VALUE;
10+
int sign = 1;
11+
if (s.charAt(i) == '-') sign = -1;
12+
if (s.charAt(i) == '-' || s.charAt(i) == '+') ++i;
13+
int res = 0, flag = Integer.MAX_VALUE / 10;
14+
for (int j = i; j < n; ++j) {
15+
if (s.charAt(j) < '0' || s.charAt(j) > '9') break;
16+
if (res > flag || (res == flag && s.charAt(j) > '7')) return sign > 0 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
17+
res = res * 10 + (s.charAt(j) - '0');
2718
}
28-
return (int) sv;
19+
return sign * res;
2920
}
30-
}
21+
}

0 commit comments

Comments
 (0)