-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution8.java
More file actions
executable file
·54 lines (54 loc) · 1.48 KB
/
Copy pathSolution8.java
File metadata and controls
executable file
·54 lines (54 loc) · 1.48 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class Solution8 {
public int myAtoi(String s) {
char []sarray = s.toCharArray();
int len = s.length(), flag = 0;
boolean run=false;
String res = "";
int q=0;
for (int i=0; i<len; i++){
if (sarray[i] == ' ' && !run)
continue;
if (sarray[i] == '-' && !run){
if (flag == 0)
{flag = -1; run = true; continue;}
else
return 0;
}
if (sarray[i] == '+' && !run){
if (flag == 0)
{flag = 1; run = true; continue;}
else
return 0;
}
if (flag==0)
flag = 1;
if (sarray[i]>=48 && sarray[i] <= 57){ // 数字
res = res + sarray[i];
run = true;
}
else if (!run)
return 0;
else
break;
}
if (!run || res.equals(""))
return 0;
if (flag == -1){
try{
q = Integer.parseInt(res);
}
catch (NumberFormatException exception){
q = (int)Math.pow(2, 31) + 1;
}
}
if (flag == 1){
try{
q = Integer.parseInt(res);
}
catch (NumberFormatException exception){
q = (int)Math.pow(2, 31);
}
}
return flag * q;
}
}