Skip to content

Commit 4e04510

Browse files
valid number
1 parent 89fff0f commit 4e04510

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

HARD/src/hard/ValidNumber.java

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package hard;
2+
/**65. Valid Number QuestionEditorial Solution My Submissions
3+
Total Accepted: 50950
4+
Total Submissions: 413050
5+
Difficulty: Hard
6+
Validate if a given string is numeric.
7+
8+
Some examples:
9+
"0" => true
10+
" 0.1 " => true
11+
"abc" => false
12+
"1 a" => false
13+
"2e10" => true
14+
Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one.*/
15+
public class ValidNumber {
16+
//strip off all leading whitespaces until encounter the first number or period
17+
//after that, only one 'e' is allowed and one '.' is allowed
18+
//also, this string could be negative, don't miss this case
19+
public boolean isNumber(String s) {
20+
s = s.trim();
21+
if (s.isEmpty())
22+
return false;
23+
int eCount = 0, periodCount = 0, index = 0, numberCount = 0;
24+
while(index < s.length()) {
25+
if(s.charAt(index) == '.') periodCount++;
26+
if((s.charAt(index) == '-') || s.charAt(index) == '+' || s.charAt(index) == '.') index++;
27+
if(periodCount >= 2) return false;
28+
else break;
29+
}
30+
if(index >= s.length()) return false;
31+
while (index < s.length()) {
32+
if ((Character.getNumericValue(s.charAt(index)) < 10 && Character.getNumericValue(s
33+
.charAt(index)) >= 0)) {
34+
index++;
35+
numberCount++;
36+
continue;
37+
} else if (s.charAt(index) == 'e') {
38+
if(eCount > 1 || numberCount == 0) return false;
39+
if (eCount < 2 && index != 0 && index != (s.length() - 1)) {
40+
eCount++;
41+
} else if (index == (s.length() - 1) || index == 0)
42+
return false;
43+
if(eCount > 1) return false;
44+
index++;
45+
//after 'e', there could be '+' or '-' as long as there are numbers after these two signs
46+
if(index < s.length() && (s.charAt(index) == '+' || s.charAt(index) == '-')) {
47+
index++;
48+
if(index >= s.length()) return false;
49+
else continue;
50+
}
51+
} else if (s.charAt(index) == '.') {
52+
if(eCount >= 1) return false;
53+
if(index-1 >= 0 && (Character.getNumericValue(s.charAt(index-1)) >= 10 || Character.getNumericValue(s
54+
.charAt(index-1)) < 0)){
55+
if(s.charAt(index-1) == '+' || s.charAt(index-1) == '-') {
56+
index++;
57+
continue;
58+
}
59+
else return false;
60+
}
61+
if(index+1 < s.length() && (Character.getNumericValue(s.charAt(index+1)) >= 10 || Character.getNumericValue(s
62+
.charAt(index+1)) < 0)){
63+
if(s.charAt(index+1) == 'e'){
64+
index++;
65+
continue;
66+
}
67+
return false;
68+
}
69+
if (periodCount < 2 && (index + 1 <= (s.length() - 1)) || index - 1 >= 0) {
70+
index++;
71+
periodCount++;
72+
}
73+
if (periodCount >= 2 || (index == 0 && index + 1 >= s.length()))
74+
return false;
75+
} else
76+
return false;
77+
}
78+
return numberCount != 0;
79+
}
80+
81+
public static void main(String...strings){
82+
ValidNumber test = new ValidNumber();
83+
// String s = "1 a";
84+
// String s = "2e10";
85+
// String s = "abc";
86+
// String s = " 0.1 ";
87+
// String s = "0";
88+
// String s = "3.";
89+
// String s = "0e";
90+
// String s = "e9";
91+
// String s = "..";
92+
// String s = ".";
93+
// String s = " -.";//should be false
94+
// String s = ".e1";
95+
// String s = "1e.";
96+
// String s = "-1.";
97+
// String s = "+++";
98+
// String s = "3";
99+
// String s = "+.8";//should be true
100+
// String s = "46.e3";//should be true
101+
// String s = "6e6.5";//should be false, i.e. after e, there should be no period
102+
// String s = "6ee69";//should be false
103+
// String s = ".e1";//should be false, i.e. there needs to be a number before 'e' appears?
104+
// String s = ".e10";//should this be true then?
105+
// String s = " 005047e+6";
106+
String s = " 4e+";
107+
System.out.println(test.isNumber(s));
108+
109+
Integer.parseInt(s);
110+
}
111+
}

0 commit comments

Comments
 (0)