Skip to content

Commit 6b262f4

Browse files
authored
feat: add rust solution to lc problem: No.2437 (doocs#1397)
Signed-off-by: xiaolatiao <1628652790@qq.com>
1 parent 64070e1 commit 6b262f4

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed

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

+55
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,61 @@ function countTime(time: string): number {
260260
}
261261
```
262262

263+
### **Rust**
264+
265+
```rust
266+
impl Solution {
267+
pub fn count_time(time: String) -> i32 {
268+
let mut ans = 0;
269+
270+
for i in 0..24 {
271+
for j in 0..60 {
272+
let mut ok = true;
273+
let t = format!("{:02}:{:02}", i, j);
274+
275+
for (k, ch) in time.chars().enumerate() {
276+
if ch != '?' && ch != t.chars().nth(k).unwrap() {
277+
ok = false;
278+
}
279+
}
280+
281+
if ok {
282+
ans += 1;
283+
}
284+
}
285+
}
286+
287+
ans
288+
}
289+
}
290+
```
291+
292+
```rust
293+
impl Solution {
294+
pub fn count_time(time: String) -> i32 {
295+
let f = |s: &str, m: usize| -> i32 {
296+
let mut cnt = 0;
297+
let first = s.chars().nth(0).unwrap();
298+
let second = s.chars().nth(1).unwrap();
299+
300+
for i in 0..m {
301+
let a = first == '?' || first.to_digit(10).unwrap() as usize == i / 10;
302+
303+
let b = second == '?' || second.to_digit(10).unwrap() as usize == i % 10;
304+
305+
if a && b {
306+
cnt += 1;
307+
}
308+
}
309+
310+
cnt
311+
};
312+
313+
f(&time[..2], 24) * f(&time[3..], 60)
314+
}
315+
}
316+
```
317+
263318
### **...**
264319

265320
```

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

+55
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,61 @@ function countTime(time: string): number {
239239
}
240240
```
241241

242+
### **Rust**
243+
244+
```rust
245+
impl Solution {
246+
pub fn count_time(time: String) -> i32 {
247+
let mut ans = 0;
248+
249+
for i in 0..24 {
250+
for j in 0..60 {
251+
let mut ok = true;
252+
let t = format!("{:02}:{:02}", i, j);
253+
254+
for (k, ch) in time.chars().enumerate() {
255+
if ch != '?' && ch != t.chars().nth(k).unwrap() {
256+
ok = false;
257+
}
258+
}
259+
260+
if ok {
261+
ans += 1;
262+
}
263+
}
264+
}
265+
266+
ans
267+
}
268+
}
269+
```
270+
271+
```rust
272+
impl Solution {
273+
pub fn count_time(time: String) -> i32 {
274+
let f = |s: &str, m: usize| -> i32 {
275+
let mut cnt = 0;
276+
let first = s.chars().nth(0).unwrap();
277+
let second = s.chars().nth(1).unwrap();
278+
279+
for i in 0..m {
280+
let a = first == '?' || first.to_digit(10).unwrap() as usize == i / 10;
281+
282+
let b = second == '?' || second.to_digit(10).unwrap() as usize == i % 10;
283+
284+
if a && b {
285+
cnt += 1;
286+
}
287+
}
288+
289+
cnt
290+
};
291+
292+
f(&time[..2], 24) * f(&time[3..], 60)
293+
}
294+
}
295+
```
296+
242297
### **...**
243298

244299
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
impl Solution {
2+
pub fn count_time(time: String) -> i32 {
3+
let f = |s: &str, m: usize| -> i32 {
4+
let mut cnt = 0;
5+
let first = s.chars().nth(0).unwrap();
6+
let second = s.chars().nth(1).unwrap();
7+
8+
for i in 0..m {
9+
let a = first == '?' || first.to_digit(10).unwrap() as usize == i / 10;
10+
11+
let b = second == '?' || second.to_digit(10).unwrap() as usize == i % 10;
12+
13+
if a && b {
14+
cnt += 1;
15+
}
16+
}
17+
18+
cnt
19+
};
20+
21+
f(&time[..2], 24) * f(&time[3..], 60)
22+
}
23+
}

0 commit comments

Comments
 (0)