Skip to content

Commit 204eab0

Browse files
committed
feat: add typescript solution to lc problem: No.2437
No.2437.Number of Valid Clock Times
1 parent c490190 commit 204eab0

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

solution/2400-2499/2437.Number of Valid Clock Times/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,29 @@ func countTime(time string) int {
153153
### **TypeScript**
154154

155155
```ts
156+
function countTime(time: string): number {
157+
let [hh, mm] = time.split(':');
158+
return count(hh, 24) * count(mm, 60);
159+
};
156160

161+
function count(str: string, limit: number): number {
162+
let [a, b] = str.split('').map(d => Number(d));
163+
let ans = 0;
164+
if (isNaN(a) && isNaN(b)) return limit;
165+
if (isNaN(a)) {
166+
for (let i = 0; i <= 9; i++) {
167+
if (i * 10 + b < limit) ans++;
168+
}
169+
return ans;
170+
}
171+
if (isNaN(b)) {
172+
for (let i = 0; i <= 9; i++) {
173+
if (a * 10 + i < limit) ans++;
174+
}
175+
return ans;
176+
}
177+
return 1;
178+
}
157179
```
158180

159181
### **...**

solution/2400-2499/2437.Number of Valid Clock Times/README_EN.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,29 @@ func countTime(time string) int {
140140
### **TypeScript**
141141

142142
```ts
143+
function countTime(time: string): number {
144+
let [hh, mm] = time.split(':');
145+
return count(hh, 24) * count(mm, 60);
146+
};
143147

148+
function count(str: string, limit: number): number {
149+
let [a, b] = str.split('').map(d => Number(d));
150+
let ans = 0;
151+
if (isNaN(a) && isNaN(b)) return limit;
152+
if (isNaN(a)) {
153+
for (let i = 0; i <= 9; i++) {
154+
if (i * 10 + b < limit) ans++;
155+
}
156+
return ans;
157+
}
158+
if (isNaN(b)) {
159+
for (let i = 0; i <= 9; i++) {
160+
if (a * 10 + i < limit) ans++;
161+
}
162+
return ans;
163+
}
164+
return 1;
165+
}
144166
```
145167

146168
### **...**
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function countTime(time: string): number {
2+
let [hh, mm] = time.split(':');
3+
return count(hh, 24) * count(mm, 60);
4+
};
5+
6+
function count(str: string, limit: number): number {
7+
let [a, b] = str.split('').map(d => Number(d));
8+
let ans = 0;
9+
if (isNaN(a) && isNaN(b)) return limit;
10+
if (isNaN(a)) {
11+
for (let i = 0; i <= 9; i++) {
12+
if (i * 10 + b < limit) ans++;
13+
}
14+
return ans;
15+
}
16+
if (isNaN(b)) {
17+
for (let i = 0; i <= 9; i++) {
18+
if (a * 10 + i < limit) ans++;
19+
}
20+
return ans;
21+
}
22+
return 1;
23+
}

0 commit comments

Comments
 (0)