-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathSolution0008.java
More file actions
30 lines (28 loc) · 1.07 KB
/
Copy pathSolution0008.java
File metadata and controls
30 lines (28 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// 8. 字符串转换整数 (atoi)
/*
1、遍历去掉空格
2、判断是否有+-符号,有则记录sign
3、遍历数字,如果算上当前数字后超过整型最大值,则根据sign标志直接返回结果,否则更新结果将当前数字加入最低位
if语句另一写法 if ((long) res * 10 + digit > Integer.MAX_VALUE) {}
4、根据sign标志返回结果
*/
class Solution {
public int myAtoi(String s) {
int n = s.length(), res = 0, index = 0, sign = 1;
char[] array = s.toCharArray();
while (index < n && array[index] == ' ') {
index++;
}
if (index < n && (array[index] == '-' || array[index] == '+')) {
sign = array[index++] == '-' ? -1 : 1;
}
while (index < n && array[index] >= '0' && array[index] <= '9') {
int digit = array[index++] - '0';
if (res > (Integer.MAX_VALUE - digit) / 10) {
return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
}
res = res * 10 + digit;
}
return res * sign;
}
}