Skip to content

Commit c471a2d

Browse files
refactor 65
1 parent e2cb6db commit c471a2d

File tree

2 files changed

+181
-122
lines changed

2 files changed

+181
-122
lines changed

src/main/java/com/fishercoder/solutions/_65.java

Lines changed: 43 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -2,136 +2,57 @@
22

33
/**
44
* 65. Valid Number
5+
*
56
* Validate if a given string is numeric.
6-
* <p>
7-
* Some examples:
8-
* "0" => true
9-
* " 0.1 " => true
10-
* "abc" => false
11-
* "1 a" => false
12-
* "2e10" => true
13-
* Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one.
7+
*
8+
* Some examples: "0" => true " 0.1 " => true "abc" => false "1 a" => false "2e10" => true
9+
*
10+
* Note: It is intended for the problem statement to be ambiguous. You should gather all
11+
* requirements up front before implementing one.
1412
*/
13+
1514
public class _65 {
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
15+
/**credit: https://discuss.leetcode.com/topic/9490/clear-java-solution-with-ifs*/
16+
public static class Solution1 {
1917
public boolean isNumber(String s) {
20-
s = s.trim();
21-
if (s.isEmpty()) {
18+
s = s.trim();
19+
20+
boolean pointSeen = false;
21+
boolean eSeen = false;
22+
boolean numberSeen = false;
23+
boolean numberAfterE = true;
24+
for (int i = 0; i < s.length(); i++) {
25+
if ('0' <= s.charAt(i) && s.charAt(i) <= '9') {
26+
numberSeen = true;
27+
numberAfterE = true;
28+
} else if (s.charAt(i) == '.') {
29+
if (eSeen || pointSeen) {
2230
return false;
23-
}
24-
int eCount = 0;
25-
int periodCount = 0;
26-
int index = 0;
27-
int numberCount = 0;
28-
while (index < s.length()) {
29-
if (s.charAt(index) == '.') {
30-
periodCount++;
31-
}
32-
if ((s.charAt(index) == '-') || s.charAt(index) == '+' || s.charAt(index) == '.') {
33-
index++;
34-
}
35-
if (periodCount >= 2) {
36-
return false;
37-
} else {
38-
break;
39-
}
40-
}
41-
if (index >= s.length()) {
31+
}
32+
pointSeen = true;
33+
} else if (s.charAt(i) == 'e') {
34+
if (eSeen || !numberSeen) {
4235
return false;
36+
}
37+
numberAfterE = false;
38+
eSeen = true;
39+
} else if (s.charAt(i) == '-' || s.charAt(i) == '+') {
40+
if (i != 0 && s.charAt(i - 1) != 'e') {
41+
return false;
42+
}
43+
} else {
44+
return false;
4345
}
44-
while (index < s.length()) {
45-
if ((Character.getNumericValue(s.charAt(index)) < 10 && Character.getNumericValue(s
46-
.charAt(index)) >= 0)) {
47-
index++;
48-
numberCount++;
49-
continue;
50-
} else if (s.charAt(index) == 'e') {
51-
if (eCount > 1 || numberCount == 0) {
52-
return false;
53-
}
54-
if (eCount < 2 && index != 0 && index != (s.length() - 1)) {
55-
eCount++;
56-
} else if (index == (s.length() - 1) || index == 0) {
57-
return false;
58-
}
59-
if (eCount > 1) {
60-
return false;
61-
}
62-
index++;
63-
//after 'e', there could be '+' or '-' as long as there are numbers after these two signs
64-
if (index < s.length() && (s.charAt(index) == '+' || s.charAt(index) == '-')) {
65-
index++;
66-
if (index >= s.length()) {
67-
return false;
68-
} else {
69-
continue;
70-
}
71-
}
72-
} else if (s.charAt(index) == '.') {
73-
if (eCount >= 1) {
74-
return false;
75-
}
76-
if (index - 1 >= 0 && (Character.getNumericValue(s.charAt(index - 1)) >= 10 || Character.getNumericValue(s
77-
.charAt(index - 1)) < 0)) {
78-
if (s.charAt(index - 1) == '+' || s.charAt(index - 1) == '-') {
79-
index++;
80-
continue;
81-
} else {
82-
return false;
83-
}
84-
}
85-
if (index + 1 < s.length() && (Character.getNumericValue(s.charAt(index + 1)) >= 10 || Character.getNumericValue(s
86-
.charAt(index + 1)) < 0)) {
87-
if (s.charAt(index + 1) == 'e') {
88-
index++;
89-
continue;
90-
}
91-
return false;
92-
}
93-
if (periodCount < 2 && (index + 1 <= (s.length() - 1)) || index - 1 >= 0) {
94-
index++;
95-
periodCount++;
96-
}
97-
if (periodCount >= 2 || (index == 0 && index + 1 >= s.length())) {
98-
return false;
99-
}
100-
} else {
101-
return false;
102-
}
103-
}
104-
return numberCount != 0;
105-
}
46+
}
10647

107-
public static void main(String... strings) {
108-
_65 test = new _65();
109-
// String s = "1 a";
110-
// String s = "2e10";
111-
// String s = "abc";
112-
// String s = " 0.1 ";
113-
// String s = "0";
114-
// String s = "3.";
115-
// String s = "0e";
116-
// String s = "e9";
117-
// String s = "..";
118-
// String s = ".";
119-
// String s = " -.";//should be false
120-
// String s = ".e1";
121-
// String s = "1e.";
122-
// String s = "-1.";
123-
// String s = "+++";
124-
// String s = "3";
125-
// String s = "+.8";//should be true
126-
// String s = "46.e3";//should be true
127-
// String s = "6e6.5";//should be false, i.e. after e, there should be no period
128-
// String s = "6ee69";//should be false
129-
// String s = ".e1";//should be false, i.e. there needs to be a number before 'e' appears?
130-
// String s = ".e10";//should this be true then?
131-
// String s = " 005047e+6";
132-
String s = " 4e+";
133-
System.out.println(test.isNumber(s));
48+
return numberSeen && numberAfterE;
49+
}
50+
}
13451

135-
Integer.parseInt(s);
52+
public static class Solution2 {
53+
/** credit: https://discuss.leetcode.com/topic/2973/java-solution-with-one-line */
54+
public boolean isNumber(String s) {
55+
return s.matches("(\\s*)[+-]?((\\.[0-9]+)|([0-9]+(\\.[0-9]*)?))(e[+-]?[0-9]+)?(\\s*)");
13656
}
57+
}
13758
}
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._65;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.TestCase.assertEquals;
8+
9+
public class _65Test {
10+
private static _65.Solution1 solution1;
11+
private static _65.Solution2 solution2;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _65.Solution1();
16+
solution2 = new _65.Solution2();
17+
}
18+
19+
@Test
20+
public void test1() {
21+
assertEquals(false, solution1.isNumber("1 a"));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
assertEquals(false, solution1.isNumber("4e+"));
27+
}
28+
29+
@Test
30+
public void test3() {
31+
assertEquals(true, solution1.isNumber("005047e+6"));
32+
}
33+
34+
@Test
35+
public void test4() {
36+
assertEquals(false, solution1.isNumber(".e10"));
37+
}
38+
39+
@Test
40+
public void test5() {
41+
assertEquals(true, solution1.isNumber("2e10"));
42+
}
43+
44+
@Test
45+
public void test6() {
46+
assertEquals(false, solution1.isNumber("abc"));
47+
}
48+
49+
@Test
50+
public void test7() {
51+
assertEquals(false, solution1.isNumber(" -."));
52+
}
53+
54+
@Test
55+
public void test8() {
56+
assertEquals(true, solution1.isNumber("+.8"));
57+
}
58+
59+
@Test
60+
public void test9() {
61+
assertEquals(false, solution1.isNumber("."));
62+
}
63+
64+
@Test
65+
public void test10() {
66+
assertEquals(false, solution1.isNumber(".e1"));
67+
}
68+
69+
@Test
70+
public void test11() {
71+
assertEquals(true, solution1.isNumber("0"));
72+
}
73+
74+
@Test
75+
public void test12() {
76+
assertEquals(false, solution1.isNumber("0e"));
77+
}
78+
79+
@Test
80+
public void test13() {
81+
assertEquals(false, solution1.isNumber("6ee69"));
82+
}
83+
84+
@Test
85+
public void test14() {
86+
assertEquals(false, solution1.isNumber("+++"));
87+
}
88+
89+
@Test
90+
public void test15() {
91+
assertEquals(false, solution1.isNumber("0e"));
92+
}
93+
94+
@Test
95+
public void test16() {
96+
assertEquals(false, solution1.isNumber("e9"));
97+
}
98+
99+
@Test
100+
public void test17() {
101+
assertEquals(true, solution1.isNumber(" 0.1 "));
102+
}
103+
104+
@Test
105+
public void test18() {
106+
assertEquals(true, solution1.isNumber("46.e3"));
107+
}
108+
109+
@Test
110+
public void test19() {
111+
assertEquals(false, solution1.isNumber(".."));
112+
}
113+
114+
@Test
115+
public void test20() {
116+
assertEquals(false, solution1.isNumber(".e1"));
117+
}
118+
119+
@Test
120+
public void test21() {
121+
assertEquals(false, solution1.isNumber(".."));
122+
}
123+
124+
@Test
125+
public void test22() {
126+
assertEquals(false, solution1.isNumber("1e."));
127+
}
128+
129+
@Test
130+
public void test24() {
131+
assertEquals(true, solution1.isNumber("-1."));
132+
}
133+
134+
@Test
135+
public void test25() {
136+
assertEquals(false, solution1.isNumber("6e6.5"));
137+
}
138+
}

0 commit comments

Comments
 (0)