Skip to content

Commit 1985280

Browse files
authored
feat: add solutions to lc problem: No.3114 (#2592)
No.3114.Latest Time You Can Obtain After Replacing Characters
1 parent 1174ea6 commit 1985280

File tree

8 files changed

+325
-1
lines changed

8 files changed

+325
-1
lines changed

solution/3100-3199/3114.Latest Time You Can Obtain After Replacing Characters/README.md

+118
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,122 @@ function findLatestTime(s: string): string {
157157

158158
<!-- tabs:end -->
159159

160+
### 方法二:逐个判断
161+
162+
我们可以逐个判断 $s$ 的每一位字符,如果是 "?" 的话,我们就根据前后的字符来确定这一位字符的值。具体地,我们有以下规则:
163+
164+
- 如果 $s[0]$ 是 "?",那么 $s[0]$ 的值应该是 "1" 或 "0",具体取决于 $s[1]$ 的值。如果 $s[1]$ 是 "?" 或者 $s[1]$ 小于 "2",那么 $s[0]$ 的值应该是 "1",否则 $s[0]$ 的值应该是 "0"。
165+
- 如果 $s[1]$ 是 "?",那么 $s[1]$ 的值应该是 "1" 或 "9",具体取决于 $s[0]$ 的值。如果 $s[0]$ 是 "1",那么 $s[1]$ 的值应该是 "1",否则 $s[1]$ 的值应该是 "9"。
166+
- 如果 $s[3]$ 是 "?",那么 $s[3]$ 的值应该是 "5"。
167+
- 如果 $s[4]$ 是 "?",那么 $s[4]$ 的值应该是 "9"。
168+
169+
时间复杂度 $O(1)$,空间复杂度 $O(1)$。
170+
171+
<!-- tabs:start -->
172+
173+
```python
174+
class Solution:
175+
def findLatestTime(self, s: str) -> str:
176+
s = list(s)
177+
if s[0] == "?":
178+
s[0] = "1" if s[1] == "?" or s[1] < "2" else "0"
179+
if s[1] == "?":
180+
s[1] = "1" if s[0] == "1" else "9"
181+
if s[3] == "?":
182+
s[3] = "5"
183+
if s[4] == "?":
184+
s[4] = "9"
185+
return "".join(s)
186+
```
187+
188+
```java
189+
class Solution {
190+
public String findLatestTime(String s) {
191+
char[] cs = s.toCharArray();
192+
if (cs[0] == '?') {
193+
cs[0] = cs[1] == '?' || cs[1] < '2' ? '1' : '0';
194+
}
195+
if (cs[1] == '?') {
196+
cs[1] = cs[0] == '1' ? '1' : '9';
197+
}
198+
if (cs[3] == '?') {
199+
cs[3] = '5';
200+
}
201+
if (cs[4] == '?') {
202+
cs[4] = '9';
203+
}
204+
return new String(cs);
205+
}
206+
}
207+
```
208+
209+
```cpp
210+
class Solution {
211+
public:
212+
string findLatestTime(string s) {
213+
if (s[0] == '?') {
214+
s[0] = s[1] == '?' || s[1] < '2' ? '1' : '0';
215+
}
216+
if (s[1] == '?') {
217+
s[1] = s[0] == '1' ? '1' : '9';
218+
}
219+
if (s[3] == '?') {
220+
s[3] = '5';
221+
}
222+
if (s[4] == '?') {
223+
s[4] = '9';
224+
}
225+
return s;
226+
}
227+
};
228+
```
229+
230+
```go
231+
func findLatestTime(s string) string {
232+
cs := []byte(s)
233+
if cs[0] == '?' {
234+
if cs[1] == '?' || cs[1] < '2' {
235+
cs[0] = '1'
236+
} else {
237+
cs[0] = '0'
238+
}
239+
}
240+
if cs[1] == '?' {
241+
if cs[0] == '1' {
242+
cs[1] = '1'
243+
} else {
244+
cs[1] = '9'
245+
}
246+
}
247+
if cs[3] == '?' {
248+
cs[3] = '5'
249+
}
250+
if cs[4] == '?' {
251+
cs[4] = '9'
252+
}
253+
return string(cs)
254+
}
255+
```
256+
257+
```ts
258+
function findLatestTime(s: string): string {
259+
const cs = s.split('');
260+
if (cs[0] === '?') {
261+
cs[0] = cs[1] === '?' || cs[1] < '2' ? '1' : '0';
262+
}
263+
if (cs[1] === '?') {
264+
cs[1] = cs[0] === '1' ? '1' : '9';
265+
}
266+
if (cs[3] === '?') {
267+
cs[3] = '5';
268+
}
269+
if (cs[4] === '?') {
270+
cs[4] = '9';
271+
}
272+
return cs.join('');
273+
}
274+
```
275+
276+
<!-- tabs:end -->
277+
160278
<!-- end -->

solution/3100-3199/3114.Latest Time You Can Obtain After Replacing Characters/README_EN.md

+118
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,122 @@ function findLatestTime(s: string): string {
153153

154154
<!-- tabs:end -->
155155

156+
### Solution 2: Judge Each Digit
157+
158+
We can judge each digit of $s$ one by one. If it is "?", we determine the value of this digit based on the characters before and after it. Specifically, we have the following rules:
159+
160+
- If $s[0]$ is "?", then the value of $s[0]$ should be "1" or "0", depending on the value of $s[1]$. If $s[1]$ is "?" or $s[1]$ is less than "2", then the value of $s[0]$ should be "1", otherwise the value of $s[0]$ should be "0".
161+
- If $s[1]$ is "?", then the value of $s[1]$ should be "1" or "9", depending on the value of $s[0]$. If $s[0]$ is "1", then the value of $s[1]$ should be "1", otherwise the value of $s[1]$ should be "9".
162+
- If $s[3]$ is "?", then the value of $s[3]$ should be "5".
163+
- If $s[4]$ is "?", then the value of $s[4]$ should be "9".
164+
165+
The time complexity is $O(1)$, and the space complexity is $O(1)$.
166+
167+
<!-- tabs:start -->
168+
169+
```python
170+
class Solution:
171+
def findLatestTime(self, s: str) -> str:
172+
s = list(s)
173+
if s[0] == "?":
174+
s[0] = "1" if s[1] == "?" or s[1] < "2" else "0"
175+
if s[1] == "?":
176+
s[1] = "1" if s[0] == "1" else "9"
177+
if s[3] == "?":
178+
s[3] = "5"
179+
if s[4] == "?":
180+
s[4] = "9"
181+
return "".join(s)
182+
```
183+
184+
```java
185+
class Solution {
186+
public String findLatestTime(String s) {
187+
char[] cs = s.toCharArray();
188+
if (cs[0] == '?') {
189+
cs[0] = cs[1] == '?' || cs[1] < '2' ? '1' : '0';
190+
}
191+
if (cs[1] == '?') {
192+
cs[1] = cs[0] == '1' ? '1' : '9';
193+
}
194+
if (cs[3] == '?') {
195+
cs[3] = '5';
196+
}
197+
if (cs[4] == '?') {
198+
cs[4] = '9';
199+
}
200+
return new String(cs);
201+
}
202+
}
203+
```
204+
205+
```cpp
206+
class Solution {
207+
public:
208+
string findLatestTime(string s) {
209+
if (s[0] == '?') {
210+
s[0] = s[1] == '?' || s[1] < '2' ? '1' : '0';
211+
}
212+
if (s[1] == '?') {
213+
s[1] = s[0] == '1' ? '1' : '9';
214+
}
215+
if (s[3] == '?') {
216+
s[3] = '5';
217+
}
218+
if (s[4] == '?') {
219+
s[4] = '9';
220+
}
221+
return s;
222+
}
223+
};
224+
```
225+
226+
```go
227+
func findLatestTime(s string) string {
228+
cs := []byte(s)
229+
if cs[0] == '?' {
230+
if cs[1] == '?' || cs[1] < '2' {
231+
cs[0] = '1'
232+
} else {
233+
cs[0] = '0'
234+
}
235+
}
236+
if cs[1] == '?' {
237+
if cs[0] == '1' {
238+
cs[1] = '1'
239+
} else {
240+
cs[1] = '9'
241+
}
242+
}
243+
if cs[3] == '?' {
244+
cs[3] = '5'
245+
}
246+
if cs[4] == '?' {
247+
cs[4] = '9'
248+
}
249+
return string(cs)
250+
}
251+
```
252+
253+
```ts
254+
function findLatestTime(s: string): string {
255+
const cs = s.split('');
256+
if (cs[0] === '?') {
257+
cs[0] = cs[1] === '?' || cs[1] < '2' ? '1' : '0';
258+
}
259+
if (cs[1] === '?') {
260+
cs[1] = cs[0] === '1' ? '1' : '9';
261+
}
262+
if (cs[3] === '?') {
263+
cs[3] = '5';
264+
}
265+
if (cs[4] === '?') {
266+
cs[4] = '9';
267+
}
268+
return cs.join('');
269+
}
270+
```
271+
272+
<!-- tabs:end -->
273+
156274
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
string findLatestTime(string s) {
4+
if (s[0] == '?') {
5+
s[0] = s[1] == '?' || s[1] < '2' ? '1' : '0';
6+
}
7+
if (s[1] == '?') {
8+
s[1] = s[0] == '1' ? '1' : '9';
9+
}
10+
if (s[3] == '?') {
11+
s[3] = '5';
12+
}
13+
if (s[4] == '?') {
14+
s[4] = '9';
15+
}
16+
return s;
17+
}
18+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
func findLatestTime(s string) string {
2+
cs := []byte(s)
3+
if cs[0] == '?' {
4+
if cs[1] == '?' || cs[1] < '2' {
5+
cs[0] = '1'
6+
} else {
7+
cs[0] = '0'
8+
}
9+
}
10+
if cs[1] == '?' {
11+
if cs[0] == '1' {
12+
cs[1] = '1'
13+
} else {
14+
cs[1] = '9'
15+
}
16+
}
17+
if cs[3] == '?' {
18+
cs[3] = '5'
19+
}
20+
if cs[4] == '?' {
21+
cs[4] = '9'
22+
}
23+
return string(cs)
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public String findLatestTime(String s) {
3+
char[] cs = s.toCharArray();
4+
if (cs[0] == '?') {
5+
cs[0] = cs[1] == '?' || cs[1] < '2' ? '1' : '0';
6+
}
7+
if (cs[1] == '?') {
8+
cs[1] = cs[0] == '1' ? '1' : '9';
9+
}
10+
if (cs[3] == '?') {
11+
cs[3] = '5';
12+
}
13+
if (cs[4] == '?') {
14+
cs[4] = '9';
15+
}
16+
return new String(cs);
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def findLatestTime(self, s: str) -> str:
3+
s = list(s)
4+
if s[0] == "?":
5+
s[0] = "1" if s[1] == "?" or s[1] < "2" else "0"
6+
if s[1] == "?":
7+
s[1] = "1" if s[0] == "1" else "9"
8+
if s[3] == "?":
9+
s[3] = "5"
10+
if s[4] == "?":
11+
s[4] = "9"
12+
return "".join(s)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function findLatestTime(s: string): string {
2+
const cs = s.split('');
3+
if (cs[0] === '?') {
4+
cs[0] = cs[1] === '?' || cs[1] < '2' ? '1' : '0';
5+
}
6+
if (cs[1] === '?') {
7+
cs[1] = cs[0] === '1' ? '1' : '9';
8+
}
9+
if (cs[3] === '?') {
10+
cs[3] = '5';
11+
}
12+
if (cs[4] === '?') {
13+
cs[4] = '9';
14+
}
15+
return cs.join('');
16+
}

solution/3100-3199/3117.Minimum Sum of Values by Dividing Array/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686

8787
### 方法一:记忆化搜索
8888

89-
我们设计一个函数 $dfs(i, j, a)$,表示从第 $i$ 个元素开始,且当前已经划分了 $j$ 个子数组,且当前待划分的子数组的按位与结果为 $a$ 的情况下,所能得到的可能的最小子数组值之和。那么答案就是 $dfs(0, 0, -1)$。
89+
我们设计一个函数 $dfs(i, j, a)$,表示从第 $i$ 个元素开始,当前已经划分了 $j$ 个子数组,且当前待划分的子数组的按位与结果为 $a$ 的情况下,所能得到的可能的最小子数组值之和。那么答案就是 $dfs(0, 0, -1)$。
9090

9191
函数 $dfs(i, j, a)$ 的执行过程如下:
9292

0 commit comments

Comments
 (0)