forked from xiaoyaoworm/Leetcode-java
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path65_validNumber.java
53 lines (44 loc) · 1.33 KB
/
65_validNumber.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
42
43
44
45
46
47
48
49
50
51
52
53
//http://blog.csdn.net/ever223/article/details/44854209
public class Solution {
public boolean isNumber(String s) {
if(s == null) return false;
s = s.trim();
if(s.length() == 0) return false;
boolean hasNum = false;
boolean hasE = false;
boolean hasDecimal = false;
int i = 0;
if(isSign(s.charAt(0))) i++;
while(i<s.length()){
char c = s.charAt(i);
if(isNum(c)){
if(!hasNum) hasNum = true;
}
else if(isDecimal(c)){
if(hasE || hasDecimal) return false;
hasDecimal = true;
}else if(isE(c)){
if(!hasNum || hasE ||(++i) >= s.length()) return false;
if(!isSign(s.charAt(i))) i--; //attention, here is not c due to (++i) in last step
hasE = true;
hasNum = false;
} else{
return false;
}
i++;
}
return hasNum;
}
public boolean isNum(char c){
return c >= '0' && c <= '9';
}
public boolean isDecimal(char c){
return c == '.';
}
public boolean isE(char c){
return c == 'e';
}
public boolean isSign(char c){
return c == '+' || c == '-';
}
}