Skip to content

Commit f9d59be

Browse files
committed
solve problem String To Integer Atoi
1 parent ba83600 commit f9d59be

File tree

5 files changed

+119
-0
lines changed

5 files changed

+119
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ All solutions will be accepted!
307307
|789|[Escape The Ghosts](https://leetcode-cn.com/problems/escape-the-ghosts/description/)|[java/py/js](./algorithms/EscapeTheGhosts)|Medium|
308308
|701|[Insert Into A Binary Search Tree](https://leetcode-cn.com/problems/insert-into-a-binary-search-tree/description/)|[java/py/js](./algorithms/InsertIntoABinarySearchTree)|Medium|
309309
|165|[Compare Version Numbers](https://leetcode-cn.com/problems/compare-version-numbers/description/)|[java/py/js](./algorithms/CompareVersionNumbers)|Medium|
310+
|8|[String To Integer Atoi](https://leetcode-cn.com/problems/string-to-integer-atoi/description/)|[java/py/js](./algorithms/StringToIntegerAtoi)|Medium|
310311

311312
# Database
312313
|#|Title|Solution|Difficulty|
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# String To Integer Atoi
2+
This problem is easy to solve
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution {
2+
public int myAtoi(String str) {
3+
str = str.trim();
4+
if (str.length() == 0)
5+
return 0;
6+
7+
int val = 0,
8+
i = 1;
9+
boolean isNegative = false;
10+
11+
char prefix = str.charAt(0);
12+
if (prefix == '+' || prefix == '-')
13+
isNegative = prefix == '-' ? true : false;
14+
else if (Character.isDigit(prefix))
15+
val = prefix - '0';
16+
else
17+
return 0;
18+
19+
int maxInt = 2147483647,
20+
minInt = -2147483648,
21+
spInt = 214748364;
22+
23+
while (i < str.length() && Character.isDigit(str.charAt(i))) {
24+
int v = str.charAt(i++) - '0';
25+
if (val > spInt)
26+
return isNegative ? minInt : maxInt;
27+
else if (val == spInt) {
28+
if (isNegative && v > 8)
29+
return minInt;
30+
else if (!isNegative && v > 7)
31+
return maxInt;
32+
}
33+
val = val * 10 + v;
34+
}
35+
36+
return isNegative ? -val : val;
37+
}
38+
}
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @param {string} str
3+
* @return {number}
4+
*/
5+
var myAtoi = function(str) {
6+
str = str.trim()
7+
if (str.length == 0)
8+
return 0
9+
10+
let val = 0,
11+
isNegative = false,
12+
i = 1
13+
14+
if (str[0] == '+' || str[0] == '-')
15+
isNegative = str[0] == '-' ? true : false
16+
else if ('0' <= str[0] && str[0] <= '9')
17+
val = str[0] - 0
18+
else
19+
return 0
20+
21+
let maxInt = 2147483647,
22+
minInt = -2147483648,
23+
spInt = 214748364
24+
25+
while (i < str.length && '0' <= str[i] && str[i] <= '9') {
26+
let v = str[i++] - 0
27+
if (val > spInt)
28+
return isNegative ? minInt : maxInt
29+
else if (val == spInt) {
30+
if (isNegative && v > 8)
31+
return minInt
32+
else if (!isNegative && v > 7)
33+
return maxInt
34+
}
35+
val = val * 10 + v
36+
}
37+
38+
return isNegative ? -val : val
39+
};
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Solution(object):
2+
def myAtoi(self, str):
3+
"""
4+
:type str: str
5+
:rtype: int
6+
"""
7+
s = str.strip()
8+
if len(s) == 0:
9+
return 0
10+
11+
val = 0
12+
is_negative = False
13+
i = 1
14+
15+
if s[0] == '+' or s[0] == '-':
16+
is_negative = False if s[0] == '+' else True
17+
elif s[0].isdigit():
18+
val = ord(s[0]) - 48
19+
else:
20+
return 0
21+
22+
max_int = 2147483647
23+
min_int = -2147483648
24+
special_int = 214748364
25+
26+
while i < len(s) and s[i].isdigit():
27+
v = ord(s[i]) - 48
28+
if val > special_int:
29+
return min_int if is_negative else max_int
30+
elif val == special_int:
31+
if is_negative and v > 8:
32+
return min_int
33+
elif not is_negative and v > 7:
34+
return max_int
35+
val = val * 10 + v
36+
i += 1
37+
38+
return -val if is_negative else val
39+

0 commit comments

Comments
 (0)