Skip to content

Commit 83ce399

Browse files
refactor 65
1 parent 2e9bbb1 commit 83ce399

File tree

1 file changed

+42
-49
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+42
-49
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,51 @@
11
package com.fishercoder.solutions;
22

3-
/**
4-
* 65. Valid Number
5-
*
6-
* Validate if a given string is numeric.
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.
12-
*/
13-
143
public class _65 {
15-
/**credit: https://discuss.leetcode.com/topic/9490/clear-java-solution-with-ifs*/
16-
public static class Solution1 {
17-
public boolean isNumber(String s) {
18-
s = s.trim();
4+
/**
5+
* credit: https://discuss.leetcode.com/topic/9490/clear-java-solution-with-ifs
6+
*/
7+
public static class Solution1 {
8+
public boolean isNumber(String s) {
9+
s = s.trim();
1910

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) {
30-
return false;
31-
}
32-
pointSeen = true;
33-
} else if (s.charAt(i) == 'e') {
34-
if (eSeen || !numberSeen) {
35-
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;
45-
}
46-
}
11+
boolean pointSeen = false;
12+
boolean eSeen = false;
13+
boolean numberSeen = false;
14+
boolean numberAfterE = true;
15+
for (int i = 0; i < s.length(); i++) {
16+
if ('0' <= s.charAt(i) && s.charAt(i) <= '9') {
17+
numberSeen = true;
18+
numberAfterE = true;
19+
} else if (s.charAt(i) == '.') {
20+
if (eSeen || pointSeen) {
21+
return false;
22+
}
23+
pointSeen = true;
24+
} else if (s.charAt(i) == 'e') {
25+
if (eSeen || !numberSeen) {
26+
return false;
27+
}
28+
numberAfterE = false;
29+
eSeen = true;
30+
} else if (s.charAt(i) == '-' || s.charAt(i) == '+') {
31+
if (i != 0 && s.charAt(i - 1) != 'e') {
32+
return false;
33+
}
34+
} else {
35+
return false;
36+
}
37+
}
4738

48-
return numberSeen && numberAfterE;
39+
return numberSeen && numberAfterE;
40+
}
4941
}
50-
}
5142

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*)");
43+
public static class Solution2 {
44+
/**
45+
* credit: https://discuss.leetcode.com/topic/2973/java-solution-with-one-line
46+
*/
47+
public boolean isNumber(String s) {
48+
return s.matches("(\\s*)[+-]?((\\.[0-9]+)|([0-9]+(\\.[0-9]*)?))(e[+-]?[0-9]+)?(\\s*)");
49+
}
5650
}
57-
}
5851
}

0 commit comments

Comments
 (0)