-
Notifications
You must be signed in to change notification settings - Fork 34
/
Solution.java
41 lines (41 loc) · 1.12 KB
/
Solution.java
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
31
32
33
34
35
36
37
38
39
40
41
public class Solution {
public int myAtoi(String str) {
char[] cs=str.toCharArray();
long ans=0;
int sign=1;
boolean signFlag=true;
int i=0;
boolean zeroFlag=true;
while (i < cs.length) {
if (zeroFlag) {
while (cs[i]==' ') {
i++;
}
zeroFlag=false;
}
if (!zeroFlag&&cs[i]==' ') {
break;
}
if (cs[i]=='-'||cs[i]=='+') {
if (signFlag) {
sign=44-cs[i];
signFlag=false;
}else{
return 0;
}
}else if(cs[i]>='0'&&cs[i]<='9'){
ans=10*ans+cs[i]-48;
if (sign==1&&ans>=Integer.MAX_VALUE) {
return Integer.MAX_VALUE;
}
if (sign==-1&&ans-1>=Integer.MAX_VALUE) {
return Integer.MIN_VALUE;
}
}else{
break;
}
i++;
}
return (int) (ans*sign);
}
}