-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValid_Number_65.java
More file actions
53 lines (49 loc) · 1.58 KB
/
Copy pathValid_Number_65.java
File metadata and controls
53 lines (49 loc) · 1.58 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
package leetcode.String_Array;
/**
* Created by sunbo_000 on 10/20/2016.
*/
/*
https://leetcode.com/problems/valid-number/
Validate if a given string is numeric.
Some examples:
"0" => true
" 0.1 " => true
"abc" => false
"1 a" => false
"2e10" => true
Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one.
*/
public class Valid_Number_65 {
public boolean isNumber(String s) {
int i = 0;
String str = s.trim();
int length = str.length();
boolean isNumeric = false;
if (i < length && (str.charAt(i) == '+' || str.charAt(i) == '-')) i++;
while (i < length && Character.isDigit(str.charAt(i))) {
i++;
isNumeric = true;
}
if (i < length && str.charAt(i) == '.') {
i++;
while (i < length && Character.isDigit(str.charAt(i))) {
isNumeric = true;
i++;
}
}
if (i < length && isNumeric && (str.charAt(i) == 'e' || str.charAt(i) == 'E')) {
i++;
isNumeric = false;
if (i < length && (str.charAt(i) == '+' || str.charAt(i) == '-')) i++;
while (i < length && Character.isDigit(str.charAt(i))) {
i++;
isNumeric = true;
}
}
return isNumeric && i == length;
}
public static void main(String[] args) {
Valid_Number_65 solution = new Valid_Number_65();
System.out.println(solution.isNumber("3"));
}
}