Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
46 lines (35 loc) · 1.49 KB

File metadata and controls

46 lines (35 loc) · 1.49 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/**
* 剑指Offer,表示数值的字符串
*/
public class IsNumericSolution {
public static void main(String[] args) {
System.out.println(isNumeric("123".toCharArray()));
}
public static boolean isNumeric(char[] str) {
boolean point = false, exp = false; // 标志小数点和指数
for (int i = 0; i < str.length; i++) {
if (str[i] == '+' || str[i] == '-') {
if (i + 1 == str.length || !(str[i + 1] >= '0' && str[i + 1] <= '9' || str[i + 1] == '.')) { // +-号后面必定为数字 或 后面为.(-.123 = -0.123)
return false;
}
if (!(i == 0 || str[i-1] == 'e' || str[i-1] == 'E')) { // +-号只出现在第一位或eE的后一位
return false;
}
} else if (str[i] == '.') {
if (point || exp || !(i + 1 < str.length && str[i + 1] >= '0' && str[i + 1] <= '9')) { // .后面必定为数字 或为最后一位(233. = 233.0)
return false;
}
point = true;
} else if (str[i] == 'e' || str[i] == 'E') {
if (exp || i + 1 == str.length || !(str[i + 1] >= '0' && str[i + 1] <= '9' || str[i + 1] == '+' || str[i + 1] == '-')) { // eE后面必定为数字或+-号
return false;
}
exp = true;
} else if (str[i] >= '0' && str[i] <= '9') {
} else {
return false;
}
}
return true;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.