You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Given a string s, return whether s is a valid number.
3
+
4
+
For example, all the following are valid numbers: "2", "0089", "-0.1", "+3.14", "4.", "-.9", "2e10", "-90E3", "3e+7", "+6e-1", "53.5e93", "-123.456e789", while the following are not valid numbers: "abc", "1a", "1e", "e3", "99e2.5", "--6", "-+3", "95a54e53".
5
+
6
+
Formally, a valid number is defined using one of the following definitions:
7
+
8
+
An integer number followed by an optional exponent.
9
+
A decimal number followed by an optional exponent.
10
+
An integer number is defined with an optional sign '-' or '+' followed by digits.
11
+
12
+
A decimal number is defined with an optional sign '-' or '+' followed by one of the following definitions:
13
+
14
+
Digits followed by a dot '.'.
15
+
Digits followed by a dot '.' followed by digits.
16
+
A dot '.' followed by digits.
17
+
An exponent is defined with an exponent notation 'e' or 'E' followed by an integer number.
18
+
19
+
The digits are defined as one or more digits.
20
+
21
+
22
+
23
+
Example 1:
24
+
25
+
Input: s = "0"
26
+
27
+
Output: true
28
+
29
+
Example 2:
30
+
31
+
Input: s = "e"
32
+
33
+
Output: false
34
+
35
+
Example 3:
36
+
37
+
Input: s = "."
38
+
39
+
Output: false
40
+
41
+
42
+
43
+
Constraints:
44
+
45
+
1 <= s.length <= 20
46
+
s consists of only English letters (both uppercase and lowercase), digits (0-9), plus '+', minus '-', or dot '.'.
47
+
*/
48
+
49
+
50
+
51
+
// My Solution:
52
+
classSolution {
53
+
publicbooleanisNumber(Strings) {
54
+
// String + Divide-and-conquer
55
+
// recommand to forward to DFA solution
56
+
intePos = -1;
57
+
for (inti=0; i<s.length(); i++) {
58
+
if (s.charAt(i) != 'e' && s.charAt(i) != 'E') continue;
0 commit comments