Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problem: No.3114 #2592

Merged
merged 1 commit into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,122 @@ function findLatestTime(s: string): string {

<!-- tabs:end -->

### 方法二:逐个判断

我们可以逐个判断 $s$ 的每一位字符,如果是 "?" 的话,我们就根据前后的字符来确定这一位字符的值。具体地,我们有以下规则:

- 如果 $s[0]$ 是 "?",那么 $s[0]$ 的值应该是 "1" 或 "0",具体取决于 $s[1]$ 的值。如果 $s[1]$ 是 "?" 或者 $s[1]$ 小于 "2",那么 $s[0]$ 的值应该是 "1",否则 $s[0]$ 的值应该是 "0"。
- 如果 $s[1]$ 是 "?",那么 $s[1]$ 的值应该是 "1" 或 "9",具体取决于 $s[0]$ 的值。如果 $s[0]$ 是 "1",那么 $s[1]$ 的值应该是 "1",否则 $s[1]$ 的值应该是 "9"。
- 如果 $s[3]$ 是 "?",那么 $s[3]$ 的值应该是 "5"。
- 如果 $s[4]$ 是 "?",那么 $s[4]$ 的值应该是 "9"。

时间复杂度 $O(1)$,空间复杂度 $O(1)$。

<!-- tabs:start -->

```python
class Solution:
def findLatestTime(self, s: str) -> str:
s = list(s)
if s[0] == "?":
s[0] = "1" if s[1] == "?" or s[1] < "2" else "0"
if s[1] == "?":
s[1] = "1" if s[0] == "1" else "9"
if s[3] == "?":
s[3] = "5"
if s[4] == "?":
s[4] = "9"
return "".join(s)
```

```java
class Solution {
public String findLatestTime(String s) {
char[] cs = s.toCharArray();
if (cs[0] == '?') {
cs[0] = cs[1] == '?' || cs[1] < '2' ? '1' : '0';
}
if (cs[1] == '?') {
cs[1] = cs[0] == '1' ? '1' : '9';
}
if (cs[3] == '?') {
cs[3] = '5';
}
if (cs[4] == '?') {
cs[4] = '9';
}
return new String(cs);
}
}
```

```cpp
class Solution {
public:
string findLatestTime(string s) {
if (s[0] == '?') {
s[0] = s[1] == '?' || s[1] < '2' ? '1' : '0';
}
if (s[1] == '?') {
s[1] = s[0] == '1' ? '1' : '9';
}
if (s[3] == '?') {
s[3] = '5';
}
if (s[4] == '?') {
s[4] = '9';
}
return s;
}
};
```

```go
func findLatestTime(s string) string {
cs := []byte(s)
if cs[0] == '?' {
if cs[1] == '?' || cs[1] < '2' {
cs[0] = '1'
} else {
cs[0] = '0'
}
}
if cs[1] == '?' {
if cs[0] == '1' {
cs[1] = '1'
} else {
cs[1] = '9'
}
}
if cs[3] == '?' {
cs[3] = '5'
}
if cs[4] == '?' {
cs[4] = '9'
}
return string(cs)
}
```

```ts
function findLatestTime(s: string): string {
const cs = s.split('');
if (cs[0] === '?') {
cs[0] = cs[1] === '?' || cs[1] < '2' ? '1' : '0';
}
if (cs[1] === '?') {
cs[1] = cs[0] === '1' ? '1' : '9';
}
if (cs[3] === '?') {
cs[3] = '5';
}
if (cs[4] === '?') {
cs[4] = '9';
}
return cs.join('');
}
```

<!-- tabs:end -->

<!-- end -->
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,122 @@ function findLatestTime(s: string): string {

<!-- tabs:end -->

### Solution 2: Judge Each Digit

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:

- 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".
- 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".
- If $s[3]$ is "?", then the value of $s[3]$ should be "5".
- If $s[4]$ is "?", then the value of $s[4]$ should be "9".

The time complexity is $O(1)$, and the space complexity is $O(1)$.

<!-- tabs:start -->

```python
class Solution:
def findLatestTime(self, s: str) -> str:
s = list(s)
if s[0] == "?":
s[0] = "1" if s[1] == "?" or s[1] < "2" else "0"
if s[1] == "?":
s[1] = "1" if s[0] == "1" else "9"
if s[3] == "?":
s[3] = "5"
if s[4] == "?":
s[4] = "9"
return "".join(s)
```

```java
class Solution {
public String findLatestTime(String s) {
char[] cs = s.toCharArray();
if (cs[0] == '?') {
cs[0] = cs[1] == '?' || cs[1] < '2' ? '1' : '0';
}
if (cs[1] == '?') {
cs[1] = cs[0] == '1' ? '1' : '9';
}
if (cs[3] == '?') {
cs[3] = '5';
}
if (cs[4] == '?') {
cs[4] = '9';
}
return new String(cs);
}
}
```

```cpp
class Solution {
public:
string findLatestTime(string s) {
if (s[0] == '?') {
s[0] = s[1] == '?' || s[1] < '2' ? '1' : '0';
}
if (s[1] == '?') {
s[1] = s[0] == '1' ? '1' : '9';
}
if (s[3] == '?') {
s[3] = '5';
}
if (s[4] == '?') {
s[4] = '9';
}
return s;
}
};
```

```go
func findLatestTime(s string) string {
cs := []byte(s)
if cs[0] == '?' {
if cs[1] == '?' || cs[1] < '2' {
cs[0] = '1'
} else {
cs[0] = '0'
}
}
if cs[1] == '?' {
if cs[0] == '1' {
cs[1] = '1'
} else {
cs[1] = '9'
}
}
if cs[3] == '?' {
cs[3] = '5'
}
if cs[4] == '?' {
cs[4] = '9'
}
return string(cs)
}
```

```ts
function findLatestTime(s: string): string {
const cs = s.split('');
if (cs[0] === '?') {
cs[0] = cs[1] === '?' || cs[1] < '2' ? '1' : '0';
}
if (cs[1] === '?') {
cs[1] = cs[0] === '1' ? '1' : '9';
}
if (cs[3] === '?') {
cs[3] = '5';
}
if (cs[4] === '?') {
cs[4] = '9';
}
return cs.join('');
}
```

<!-- tabs:end -->

<!-- end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
public:
string findLatestTime(string s) {
if (s[0] == '?') {
s[0] = s[1] == '?' || s[1] < '2' ? '1' : '0';
}
if (s[1] == '?') {
s[1] = s[0] == '1' ? '1' : '9';
}
if (s[3] == '?') {
s[3] = '5';
}
if (s[4] == '?') {
s[4] = '9';
}
return s;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
func findLatestTime(s string) string {
cs := []byte(s)
if cs[0] == '?' {
if cs[1] == '?' || cs[1] < '2' {
cs[0] = '1'
} else {
cs[0] = '0'
}
}
if cs[1] == '?' {
if cs[0] == '1' {
cs[1] = '1'
} else {
cs[1] = '9'
}
}
if cs[3] == '?' {
cs[3] = '5'
}
if cs[4] == '?' {
cs[4] = '9'
}
return string(cs)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
public String findLatestTime(String s) {
char[] cs = s.toCharArray();
if (cs[0] == '?') {
cs[0] = cs[1] == '?' || cs[1] < '2' ? '1' : '0';
}
if (cs[1] == '?') {
cs[1] = cs[0] == '1' ? '1' : '9';
}
if (cs[3] == '?') {
cs[3] = '5';
}
if (cs[4] == '?') {
cs[4] = '9';
}
return new String(cs);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution:
def findLatestTime(self, s: str) -> str:
s = list(s)
if s[0] == "?":
s[0] = "1" if s[1] == "?" or s[1] < "2" else "0"
if s[1] == "?":
s[1] = "1" if s[0] == "1" else "9"
if s[3] == "?":
s[3] = "5"
if s[4] == "?":
s[4] = "9"
return "".join(s)
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function findLatestTime(s: string): string {
const cs = s.split('');
if (cs[0] === '?') {
cs[0] = cs[1] === '?' || cs[1] < '2' ? '1' : '0';
}
if (cs[1] === '?') {
cs[1] = cs[0] === '1' ? '1' : '9';
}
if (cs[3] === '?') {
cs[3] = '5';
}
if (cs[4] === '?') {
cs[4] = '9';
}
return cs.join('');
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@

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

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

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

Expand Down
Loading