-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidNumber.java
52 lines (51 loc) · 1.54 KB
/
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
package brian;
/**
* Created by brian on 8/18/17.
*/
public class ValidNumber {
public boolean isNumber(String s) {
/*
頭尾空白字元 => 需處理
小數點 => 需處理
科學記號 => 需處理
string 有數字也有其他字元 => return false
正負號 => 需處理
非十進位數字(ex: 0xFF) => return false
兩數字中間有空白 => return false
*/
// s = " +20.123 "
// i
int i = 0, n = s.length();
while (i < n && s.charAt(i) == ' ') i++;
if (i < n && (s.charAt(i) == '+' || s.charAt(i) == '-')) i++;
boolean isNumeric = false;
while (i < n && Character.isDigit(s.charAt(i))) {
i++;
isNumeric = true;
}
// "2."
// ".2"
if (i < n && (s.charAt(i) == '.')) {
i++;
while (i < n && Character.isDigit(s.charAt(i))) {
i++;
isNumeric = true;
}
}
// s = "2e+10"
// s = "2e-10"
// s = "2e"
// s = "e2"
if (isNumeric && i < n && (s.charAt(i) == 'e')) {
i++;
isNumeric = false;
if (i < n && (s.charAt(i) == '+' || s.charAt(i) == '-')) i++;
while (i < n && Character.isDigit(s.charAt(i))) {
i++;
isNumeric = true;
}
}
while (i < n && s.charAt(i) == ' ') i++;
return isNumeric && i == n;
}
}