Skip to content

Commit b0f14cd

Browse files
committed
feat: add solutions to lc problem: No.0681
No.0681.Next Closest Time
1 parent c4e0218 commit b0f14cd

File tree

4 files changed

+234
-4
lines changed

4 files changed

+234
-4
lines changed

solution/0600-0699/0681.Next Closest Time/README.md

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,91 @@
5454
<!-- 这里可写当前语言的特殊实现逻辑 -->
5555

5656
```python
57-
57+
class Solution:
58+
def nextClosestTime(self, time: str) -> str:
59+
def check(t):
60+
h, m = int(t[:2]), int(t[2:])
61+
return 0 <= h < 24 and 0 <= m < 60
62+
63+
def dfs(curr):
64+
if len(curr) == 4:
65+
if not check(curr):
66+
return
67+
nonlocal ans, d
68+
p = int(curr[:2]) * 60 + int(curr[2:])
69+
if t < p < t + d:
70+
d = p - t
71+
ans = curr[:2] + ':' + curr[2:]
72+
return
73+
for c in s:
74+
dfs(curr + c)
75+
76+
s = {c for c in time if c != ':'}
77+
t = int(time[:2]) * 60 + int(time[3:])
78+
d = float('inf')
79+
ans = None
80+
dfs('')
81+
if ans is None:
82+
mi = min(int(c) for c in s)
83+
ans = f'{mi}{mi}:{mi}{mi}'
84+
return ans
5885
```
5986

6087
### **Java**
6188

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

6491
```java
65-
92+
class Solution {
93+
private int t;
94+
private int d;
95+
private String ans;
96+
private Set<Character> s;
97+
98+
public String nextClosestTime(String time) {
99+
t = Integer.parseInt(time.substring(0, 2)) * 60 + Integer.parseInt(time.substring(3));
100+
d = Integer.MAX_VALUE;
101+
s = new HashSet<>();
102+
char mi = 'z';
103+
for (char c : time.toCharArray()) {
104+
if (c != ':') {
105+
s.add(c);
106+
if (c < mi) {
107+
mi = c;
108+
}
109+
}
110+
}
111+
ans = null;
112+
dfs("");
113+
if (ans == null) {
114+
ans = "" + mi + mi + ":" + mi + mi;
115+
}
116+
return ans;
117+
}
118+
119+
private void dfs(String curr) {
120+
if (curr.length() == 4) {
121+
if (!check(curr)) {
122+
return;
123+
}
124+
int p = Integer.parseInt(curr.substring(0, 2)) * 60 + Integer.parseInt(curr.substring(2));
125+
if (p > t && p - t < d) {
126+
d = p - t;
127+
ans = curr.substring(0, 2) + ":" + curr.substring(2);
128+
}
129+
return;
130+
}
131+
for (char c : s) {
132+
dfs(curr + c);
133+
}
134+
}
135+
136+
private boolean check(String t) {
137+
int h = Integer.parseInt(t.substring(0, 2));
138+
int m = Integer.parseInt(t.substring(2));
139+
return 0 <= h && h < 24 && 0 <= m && m < 60;
140+
}
141+
}
66142
```
67143

68144
### **...**

solution/0600-0699/0681.Next Closest Time/README_EN.md

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,89 @@ It may be assumed that the returned time is next day&#39;s time since it is smal
4444
### **Python3**
4545

4646
```python
47-
47+
class Solution:
48+
def nextClosestTime(self, time: str) -> str:
49+
def check(t):
50+
h, m = int(t[:2]), int(t[2:])
51+
return 0 <= h < 24 and 0 <= m < 60
52+
53+
def dfs(curr):
54+
if len(curr) == 4:
55+
if not check(curr):
56+
return
57+
nonlocal ans, d
58+
p = int(curr[:2]) * 60 + int(curr[2:])
59+
if t < p < t + d:
60+
d = p - t
61+
ans = curr[:2] + ':' + curr[2:]
62+
return
63+
for c in s:
64+
dfs(curr + c)
65+
66+
s = {c for c in time if c != ':'}
67+
t = int(time[:2]) * 60 + int(time[3:])
68+
d = float('inf')
69+
ans = None
70+
dfs('')
71+
if ans is None:
72+
mi = min(int(c) for c in s)
73+
ans = f'{mi}{mi}:{mi}{mi}'
74+
return ans
4875
```
4976

5077
### **Java**
5178

5279
```java
53-
80+
class Solution {
81+
private int t;
82+
private int d;
83+
private String ans;
84+
private Set<Character> s;
85+
86+
public String nextClosestTime(String time) {
87+
t = Integer.parseInt(time.substring(0, 2)) * 60 + Integer.parseInt(time.substring(3));
88+
d = Integer.MAX_VALUE;
89+
s = new HashSet<>();
90+
char mi = 'z';
91+
for (char c : time.toCharArray()) {
92+
if (c != ':') {
93+
s.add(c);
94+
if (c < mi) {
95+
mi = c;
96+
}
97+
}
98+
}
99+
ans = null;
100+
dfs("");
101+
if (ans == null) {
102+
ans = "" + mi + mi + ":" + mi + mi;
103+
}
104+
return ans;
105+
}
106+
107+
private void dfs(String curr) {
108+
if (curr.length() == 4) {
109+
if (!check(curr)) {
110+
return;
111+
}
112+
int p = Integer.parseInt(curr.substring(0, 2)) * 60 + Integer.parseInt(curr.substring(2));
113+
if (p > t && p - t < d) {
114+
d = p - t;
115+
ans = curr.substring(0, 2) + ":" + curr.substring(2);
116+
}
117+
return;
118+
}
119+
for (char c : s) {
120+
dfs(curr + c);
121+
}
122+
}
123+
124+
private boolean check(String t) {
125+
int h = Integer.parseInt(t.substring(0, 2));
126+
int m = Integer.parseInt(t.substring(2));
127+
return 0 <= h && h < 24 && 0 <= m && m < 60;
128+
}
129+
}
54130
```
55131

56132
### **...**
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
class Solution {
2+
private int t;
3+
private int d;
4+
private String ans;
5+
private Set<Character> s;
6+
7+
public String nextClosestTime(String time) {
8+
t = Integer.parseInt(time.substring(0, 2)) * 60 + Integer.parseInt(time.substring(3));
9+
d = Integer.MAX_VALUE;
10+
s = new HashSet<>();
11+
char mi = 'z';
12+
for (char c : time.toCharArray()) {
13+
if (c != ':') {
14+
s.add(c);
15+
if (c < mi) {
16+
mi = c;
17+
}
18+
}
19+
}
20+
ans = null;
21+
dfs("");
22+
if (ans == null) {
23+
ans = "" + mi + mi + ":" + mi + mi;
24+
}
25+
return ans;
26+
}
27+
28+
private void dfs(String curr) {
29+
if (curr.length() == 4) {
30+
if (!check(curr)) {
31+
return;
32+
}
33+
int p = Integer.parseInt(curr.substring(0, 2)) * 60 + Integer.parseInt(curr.substring(2));
34+
if (p > t && p - t < d) {
35+
d = p - t;
36+
ans = curr.substring(0, 2) + ":" + curr.substring(2);
37+
}
38+
return;
39+
}
40+
for (char c : s) {
41+
dfs(curr + c);
42+
}
43+
}
44+
45+
private boolean check(String t) {
46+
int h = Integer.parseInt(t.substring(0, 2));
47+
int m = Integer.parseInt(t.substring(2));
48+
return 0 <= h && h < 24 && 0 <= m && m < 60;
49+
}
50+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution:
2+
def nextClosestTime(self, time: str) -> str:
3+
def check(t):
4+
h, m = int(t[:2]), int(t[2:])
5+
return 0 <= h < 24 and 0 <= m < 60
6+
7+
def dfs(curr):
8+
if len(curr) == 4:
9+
if not check(curr):
10+
return
11+
nonlocal ans, d
12+
p = int(curr[:2]) * 60 + int(curr[2:])
13+
if t < p < t + d:
14+
d = p - t
15+
ans = curr[:2] + ':' + curr[2:]
16+
return
17+
for c in s:
18+
dfs(curr + c)
19+
20+
s = {c for c in time if c != ':'}
21+
t = int(time[:2]) * 60 + int(time[3:])
22+
d = float('inf')
23+
ans = None
24+
dfs('')
25+
if ans is None:
26+
mi = min(int(c) for c in s)
27+
ans = f'{mi}{mi}:{mi}{mi}'
28+
return ans

0 commit comments

Comments
 (0)