Skip to content

Commit 3b5fcf4

Browse files
committedOct 16, 2022
feat: add solutions to lc problems: No.2437~2440
* No.2437.Number of Valid Clock Times * No.2438.Range Product Queries of Powers * No.2439.Minimize Maximum of Array * No.2440.Create Components With Same Value
1 parent d83da51 commit 3b5fcf4

File tree

29 files changed

+1383
-15
lines changed

29 files changed

+1383
-15
lines changed
 

‎solution/0300-0399/0336.Palindrome Pairs/README.md

-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@
3232
<strong>输出:</strong>[[0,1],[1,0]]
3333
</pre>
3434

35-
36-
3735
<p><strong>提示:</strong></p>
3836

3937
<ul>

‎solution/0500-0599/0554.Brick Wall/README.md

-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
<strong>输出:</strong>3
2929
</pre>
3030

31-
32-
3331
<p><strong>提示:</strong></p>
3432

3533
<ul>

‎solution/0700-0799/0775.Global and Local Inversions/README.md

-2
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@
4242
<strong>解释:</strong>有 2 个全局倒置,和 1 个局部倒置。
4343
</pre>
4444

45-
46-
4745
<p><strong>提示:</strong></p>
4846

4947
<ul>

‎solution/0800-0899/0830.Positions of Large Groups/README.md

-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@
4848
<strong>输出:</strong>[]
4949
</pre>
5050

51-
52-
5351
<p><strong>提示:</strong></p>
5452

5553
<ul>

‎solution/1400-1499/1409.Queries on a Permutation With Key/README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<li>In the beginning, you have the permutation <code>P=[1,2,3,...,m]</code>.</li>
1111

1212
<li>For the current <code>i</code>, find the position of <code>queries[i]</code> in the permutation <code>P</code> (<strong>indexing from 0</strong>) and then move this at the beginning of the permutation <code>P.</code>&nbsp;Notice that the position of <code>queries[i]</code> in <code>P</code> is the result for <code>queries[i]</code>.</li>
13+
1314
</ul>
1415

1516
<p>Return an array containing the result for the given <code>queries</code>.</p>

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

+83-1
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,104 @@
5050

5151
<!-- 这里可写通用的实现逻辑 -->
5252

53+
**方法一:枚举**
54+
55+
直接枚举所有时间,判断是否匹配 `time` 字符串,如果匹配则计数加一。
56+
57+
时间复杂度 $O(24 \times 60)$。
58+
5359
<!-- tabs:start -->
5460

5561
### **Python3**
5662

5763
<!-- 这里可写当前语言的特殊实现逻辑 -->
5864

5965
```python
60-
66+
class Solution:
67+
def countTime(self, time: str) -> int:
68+
def check(s, t):
69+
for a, b in zip(s, t):
70+
if a != b and b != '?':
71+
return 0
72+
return 1
73+
74+
return sum(
75+
check(f'{h:02d}:{m:02d}', time) for h in range(24) for m in range(60)
76+
)
6177
```
6278

6379
### **Java**
6480

6581
<!-- 这里可写当前语言的特殊实现逻辑 -->
6682

6783
```java
84+
class Solution {
85+
public int countTime(String time) {
86+
int ans = 0;
87+
for (int h = 0; h < 24; ++h) {
88+
for (int m = 0; m < 60; ++m) {
89+
String s = String.format("%02d:%02d", h, m);
90+
int ok = 1;
91+
for (int i = 0; i < 5; ++i) {
92+
if (s.charAt(i) != time.charAt(i) && time.charAt(i) != '?') {
93+
ok = 0;
94+
break;
95+
}
96+
}
97+
ans += ok;
98+
}
99+
}
100+
return ans;
101+
}
102+
}
103+
```
104+
105+
### **C++**
106+
107+
```cpp
108+
class Solution {
109+
public:
110+
int countTime(string time) {
111+
int ans = 0;
112+
for (int h = 0; h < 24; ++h) {
113+
for (int m = 0; m < 60; ++m) {
114+
char s[20];
115+
sprintf(s, "%02d:%02d", h, m);
116+
int ok = 1;
117+
for (int i = 0; i < 5; ++i) {
118+
if (s[i] != time[i] && time[i] != '?') {
119+
ok = 0;
120+
break;
121+
}
122+
}
123+
ans += ok;
124+
}
125+
}
126+
return ans;
127+
}
128+
};
129+
```
68130
131+
### **Go**
132+
133+
```go
134+
func countTime(time string) int {
135+
ans := 0
136+
for h := 0; h < 24; h++ {
137+
for m := 0; m < 60; m++ {
138+
s := fmt.Sprintf("%02d:%02d", h, m)
139+
ok := 1
140+
for i := 0; i < 5; i++ {
141+
if s[i] != time[i] && time[i] != '?' {
142+
ok = 0
143+
break
144+
}
145+
}
146+
ans += ok
147+
}
148+
}
149+
return ans
150+
}
69151
```
70152

71153
### **TypeScript**

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

+77-1
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,89 @@
5252
### **Python3**
5353

5454
```python
55-
55+
class Solution:
56+
def countTime(self, time: str) -> int:
57+
def check(s, t):
58+
for a, b in zip(s, t):
59+
if a != b and b != '?':
60+
return 0
61+
return 1
62+
63+
return sum(
64+
check(f'{h:02d}:{m:02d}', time) for h in range(24) for m in range(60)
65+
)
5666
```
5767

5868
### **Java**
5969

6070
```java
71+
class Solution {
72+
public int countTime(String time) {
73+
int ans = 0;
74+
for (int h = 0; h < 24; ++h) {
75+
for (int m = 0; m < 60; ++m) {
76+
String s = String.format("%02d:%02d", h, m);
77+
int ok = 1;
78+
for (int i = 0; i < 5; ++i) {
79+
if (s.charAt(i) != time.charAt(i) && time.charAt(i) != '?') {
80+
ok = 0;
81+
break;
82+
}
83+
}
84+
ans += ok;
85+
}
86+
}
87+
return ans;
88+
}
89+
}
90+
```
91+
92+
### **C++**
93+
94+
```cpp
95+
class Solution {
96+
public:
97+
int countTime(string time) {
98+
int ans = 0;
99+
for (int h = 0; h < 24; ++h) {
100+
for (int m = 0; m < 60; ++m) {
101+
char s[20];
102+
sprintf(s, "%02d:%02d", h, m);
103+
int ok = 1;
104+
for (int i = 0; i < 5; ++i) {
105+
if (s[i] != time[i] && time[i] != '?') {
106+
ok = 0;
107+
break;
108+
}
109+
}
110+
ans += ok;
111+
}
112+
}
113+
return ans;
114+
}
115+
};
116+
```
61117
118+
### **Go**
119+
120+
```go
121+
func countTime(time string) int {
122+
ans := 0
123+
for h := 0; h < 24; h++ {
124+
for m := 0; m < 60; m++ {
125+
s := fmt.Sprintf("%02d:%02d", h, m)
126+
ok := 1
127+
for i := 0; i < 5; i++ {
128+
if s[i] != time[i] && time[i] != '?' {
129+
ok = 0
130+
break
131+
}
132+
}
133+
ans += ok
134+
}
135+
}
136+
return ans
137+
}
62138
```
63139

64140
### **TypeScript**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
int countTime(string time) {
4+
int ans = 0;
5+
for (int h = 0; h < 24; ++h) {
6+
for (int m = 0; m < 60; ++m) {
7+
char s[20];
8+
sprintf(s, "%02d:%02d", h, m);
9+
int ok = 1;
10+
for (int i = 0; i < 5; ++i) {
11+
if (s[i] != time[i] && time[i] != '?') {
12+
ok = 0;
13+
break;
14+
}
15+
}
16+
ans += ok;
17+
}
18+
}
19+
return ans;
20+
}
21+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
func countTime(time string) int {
2+
ans := 0
3+
for h := 0; h < 24; h++ {
4+
for m := 0; m < 60; m++ {
5+
s := fmt.Sprintf("%02d:%02d", h, m)
6+
ok := 1
7+
for i := 0; i < 5; i++ {
8+
if s[i] != time[i] && time[i] != '?' {
9+
ok = 0
10+
break
11+
}
12+
}
13+
ans += ok
14+
}
15+
}
16+
return ans
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int countTime(String time) {
3+
int ans = 0;
4+
for (int h = 0; h < 24; ++h) {
5+
for (int m = 0; m < 60; ++m) {
6+
String s = String.format("%02d:%02d", h, m);
7+
int ok = 1;
8+
for (int i = 0; i < 5; ++i) {
9+
if (s.charAt(i) != time.charAt(i) && time.charAt(i) != '?') {
10+
ok = 0;
11+
break;
12+
}
13+
}
14+
ans += ok;
15+
}
16+
}
17+
return ans;
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def countTime(self, time: str) -> int:
3+
def check(s, t):
4+
for a, b in zip(s, t):
5+
if a != b and b != '?':
6+
return 0
7+
return 1
8+
9+
return sum(
10+
check(f'{h:02d}:{m:02d}', time) for h in range(24) for m in range(60)
11+
)

0 commit comments

Comments
 (0)