|
2 | 2 |
|
3 | 3 | /**
|
4 | 4 | * 65. Valid Number
|
| 5 | + * |
5 | 6 | * 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. |
14 | 12 | */
|
| 13 | + |
15 | 14 | 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 { |
19 | 17 | 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) { |
22 | 30 | 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) { |
42 | 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; |
43 | 45 | }
|
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 | + } |
106 | 47 |
|
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 | + } |
134 | 51 |
|
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*)"); |
136 | 56 | }
|
| 57 | + } |
137 | 58 | }
|
0 commit comments