-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeetcode065.java
85 lines (69 loc) · 2.16 KB
/
Leetcode065.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package test;
//简单
public class Leetcode065 {
public boolean isValid(String a) {
boolean isDotUsed = false;
int i = 0;
while (i<a.length() && (a.charAt(i) < '0' || a.charAt(i) > '9')) {
i++;
}
if (i == a.length())
return false;
for (int j=0; j<i-1; j++) {
if (a.charAt(j) != '-' && a.charAt(j) != '+')
return false;
}
if (i-1 >= 0) {
if (a.charAt(i-1) == '.')
isDotUsed = true;
else if (a.charAt(i-1) != '+' && a.charAt(i-1) != '-')
return false;
}
for (int j=i+1; j<a.length(); j++) {
if (a.charAt(j) == '.') {
if (isDotUsed)
return false;
isDotUsed = true;
} else if (a.charAt(j) < '0' || a.charAt(j) > '9')
return false;
}
return true;
}
public boolean isValidInt(String a) {
int i = 0;
while (i<a.length() && (a.charAt(i) < '0' || a.charAt(i) > '9')) {
i++;
}
if (i == a.length())
return false;
for (int j=0; j<i; j++) {
if (a.charAt(j) != '-' && a.charAt(j) != '+')
return false;
}
for (int j=i; j<a.length(); j++) {
if (a.charAt(j) < '0' || a.charAt(j) > '9')
return false;
}
return true;
}
public boolean isNumber(String s) {
s = s.trim();
if (s.length() == 0)
return false;
for (int i=0; i<s.length(); i++) {
if (s.charAt(i) != 'e')
continue;
else {
if (i == s.length()-1)
return false;
if (isValid(s.substring(0, i)) && isValidInt(s.substring(i+1, s.length()))) {
return true;
}
return false;
}
}
if (isValid(s.substring(0, s.length())))
return true;
return false;
}
}