Skip to content

Commit b06a11b

Browse files
committed
lc q65 round 1
1 parent f3f9e84 commit b06a11b

File tree

2 files changed

+106
-2
lines changed

2 files changed

+106
-2
lines changed

Leetcode Practices/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@
119119
- [x] 157 Read N Characters Given Read4
120120
- [ ] 158 Read N Characters Given Read4 II - Call multiple times
121121
- [x] 68 Text Justification
122-
- [ ] 65 Valid Number
122+
- [x] 65 Valid Number
123123
### Substring
124124
- [x] 76 Minimum Window Substring Sliding Window
125125
- [ ] 30 Substring with Concatenation of All Words Sliding Window
@@ -564,7 +564,7 @@
564564
- [x] 60 Permutation Sequence
565565
- [x] 62 Unique Paths
566566
- [x] 64 Minimum Path Sum
567-
- [ ] 65 Valid Number
567+
- [x] 65 Valid Number
568568
- [x] 66 Plus One
569569
- [x] 67 Add Binary
570570
- [x] 68 Text Justification
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
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+
class Solution {
53+
public boolean isNumber(String s) {
54+
// String + Divide-and-conquer
55+
// recommand to forward to DFA solution
56+
int ePos = -1;
57+
for (int i=0; i<s.length(); i++) {
58+
if (s.charAt(i) != 'e' && s.charAt(i) != 'E') continue;
59+
ePos = i; break;
60+
}
61+
if (ePos == -1) return isDecimal(s);
62+
if (s.length() == 1) return false;
63+
String s1 = s.substring(0, ePos), s2 = s.substring(ePos + 1);
64+
return isDecimal(s1) && isInteger(s2);
65+
}
66+
67+
public boolean isDecimal(String s) { // include isInteger
68+
if (s.isBlank()) return false;
69+
int dotPos = -1;
70+
for (int i=0; i<s.length(); i++) {
71+
if (s.charAt(i) != '.') continue;
72+
dotPos = i; break;
73+
}
74+
if (dotPos == -1) return isInteger(s);
75+
if (s.length() == 1) return false;
76+
String s1 = s.substring(0, dotPos), s2 = s.substring(dotPos + 1);
77+
if ((isInteger(s1) || isSign(s1) || s1.isBlank()) && isPureInteger(s2)) return true;
78+
if (isInteger(s1) && s2.isBlank()) return true;
79+
return false;
80+
}
81+
82+
public boolean isInteger(String s) {
83+
if (s.isBlank()) return false;
84+
for (int i=0; i<s.length(); i++) {
85+
if (i == 0 && (s.charAt(i) == '-' || s.charAt(i) == '+') && s.length() != 1) continue;
86+
if ('0' <= s.charAt(i) && s.charAt(i) <= '9') continue;
87+
return false;
88+
}
89+
return true;
90+
}
91+
92+
public boolean isPureInteger(String s) {
93+
if (s.isBlank()) return false;
94+
for (int i=0; i<s.length(); i++) {
95+
if ('0' <= s.charAt(i) && s.charAt(i) <= '9') continue;
96+
return false;
97+
}
98+
return true;
99+
}
100+
101+
public boolean isSign(String s) {
102+
return s.equals("+") || s.equals("-");
103+
}
104+
}

0 commit comments

Comments
 (0)