Skip to content

Commit c0c3ffe

Browse files
committed
feat: update solutions to lc problems: No.0913,1576
* No.0913.Cat and Mouse * No.1576.Replace All 's to Avoid Consecutive Repeating Characters
1 parent 51837a3 commit c0c3ffe

File tree

9 files changed

+178
-102
lines changed

9 files changed

+178
-102
lines changed

solution/0900-0999/0913.Cat and Mouse/README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,7 @@ public:
200200
int catMouseGame(vector<vector<int>>& graph) {
201201
int n = graph.size();
202202
this->graph = graph;
203-
memo.resize(n, vector<vector<int>>(n, vector<int>(2 * n + 10)));
204-
for (int i = 0; i < memo.size(); ++i)
205-
for (int j = 0; j < memo[0].size(); ++j)
206-
for (int k = 0; k < memo[0][0].size(); ++k)
207-
memo[i][j][k] = -1;
203+
memo.resize(n, vector<vector<int>>(n, vector<int>(2 * n + 10, -1)));
208204
return dfs(1, 2, 0);
209205
}
210206

solution/0900-0999/0913.Cat and Mouse/README_EN.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,7 @@ public:
198198
int catMouseGame(vector<vector<int>>& graph) {
199199
int n = graph.size();
200200
this->graph = graph;
201-
memo.resize(n, vector<vector<int>>(n, vector<int>(2 * n + 10)));
202-
for (int i = 0; i < memo.size(); ++i)
203-
for (int j = 0; j < memo[0].size(); ++j)
204-
for (int k = 0; k < memo[0][0].size(); ++k)
205-
memo[i][j][k] = -1;
201+
memo.resize(n, vector<vector<int>>(n, vector<int>(2 * n + 10, -1)));
206202
return dfs(1, 2, 0);
207203
}
208204

solution/0900-0999/0913.Cat and Mouse/Solution.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@ class Solution {
66
int catMouseGame(vector<vector<int>>& graph) {
77
int n = graph.size();
88
this->graph = graph;
9-
memo.resize(n, vector<vector<int>>(n, vector<int>(2 * n + 10)));
10-
for (int i = 0; i < memo.size(); ++i)
11-
for (int j = 0; j < memo[0].size(); ++j)
12-
for (int k = 0; k < memo[0][0].size(); ++k)
13-
memo[i][j][k] = -1;
9+
memo.resize(n, vector<vector<int>>(n, vector<int>(2 * n + 10, -1)));
1410
return dfs(1, 2, 0);
1511
}
1612

solution/1500-1599/1576.Replace All 's to Avoid Consecutive Repeating Characters/README.md

Lines changed: 60 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,17 @@
6767
```python
6868
class Solution:
6969
def modifyString(self, s: str) -> str:
70-
s = list(s)
71-
for i in range(len(s)):
72-
if s[i] == '?':
73-
ahead = ' ' if i == 0 else s[i - 1]
74-
behind = ' ' if i == len(s) - 1 else s[i + 1]
75-
for c in string.ascii_lowercase:
76-
if c != ahead and c != behind:
77-
s[i] = c
78-
break
79-
return "".join(s)
70+
ans = list(s)
71+
for i, c in enumerate(ans):
72+
if c == '?':
73+
for cc in 'abc':
74+
if i > 0 and ans[i - 1] == cc:
75+
continue
76+
if i < len(s) - 1 and ans[i + 1] == cc:
77+
continue
78+
ans[i] = cc
79+
break
80+
return ''.join(ans)
8081
```
8182

8283
### **Java**
@@ -87,40 +88,70 @@ class Solution:
8788
class Solution {
8889
public String modifyString(String s) {
8990
char[] chars = s.toCharArray();
90-
for (int i = 0; i < chars.length; i++) {
91-
if (chars[i] == '?') {
92-
// 前面的字符
93-
char ahead = i == 0 ? ' ' : chars[i - 1];
94-
// 后面的字符
95-
char behind = i == chars.length - 1 ? ' ' : chars[i + 1];
96-
char temp = 'a';
97-
while (temp == ahead || temp == behind) {
98-
temp++;
91+
for (int i = 0; i < chars.length; ++i) {
92+
char c = chars[i];
93+
if (c == '?') {
94+
for (char cc = 'a'; cc <= 'c'; ++cc) {
95+
if (i > 0 && chars[i - 1] == cc) {
96+
continue;
97+
}
98+
if (i < chars.length - 1 && chars[i + 1] == cc) {
99+
continue;
100+
}
101+
chars[i] = cc;
102+
break;
99103
}
100-
chars[i] = temp;
101104
}
102105
}
103-
return new String(chars);
106+
return String.valueOf(chars);
104107
}
105108
}
106109
```
107110

111+
### **C++**
112+
113+
```cpp
114+
class Solution {
115+
public:
116+
string modifyString(string s) {
117+
for (int i = 0; i < s.size(); ++i)
118+
{
119+
if (s[i] == '?')
120+
{
121+
for (char cc : "abc")
122+
{
123+
if (i > 0 && s[i - 1] == cc) continue;
124+
if (i < s.size() - 1 && s[i + 1] == cc) continue;
125+
s[i] = cc;
126+
break;
127+
}
128+
}
129+
}
130+
return s;
131+
}
132+
};
133+
```
134+
108135
### **Go**
109136
110137
```go
111138
func modifyString(s string) string {
112-
data := []byte(" " + s + " ")
113-
for i, c := range data {
114-
if c == byte('?') {
115-
ahead, behind := data[i-1], data[i+1]
116-
for t := byte('a'); t <= byte('z'); t++ {
117-
if t != ahead && t != behind {
118-
data[i] = t
139+
ans := []byte(s)
140+
for i, c := range ans {
141+
if c == '?' {
142+
for cc := byte('a'); cc <= 'c'; cc++ {
143+
if i > 0 && ans[i-1] == cc {
144+
continue
145+
}
146+
if i < len(s)-1 && ans[i+1] == cc {
147+
continue
119148
}
149+
ans[i] = cc
150+
break
120151
}
121152
}
122153
}
123-
return string(data[1 : len(data)-1])
154+
return string(ans)
124155
}
125156
```
126157

solution/1500-1599/1576.Replace All 's to Avoid Consecutive Repeating Characters/README_EN.md

Lines changed: 60 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,17 @@
5757
```python
5858
class Solution:
5959
def modifyString(self, s: str) -> str:
60-
s = list(s)
61-
for i in range(len(s)):
62-
if s[i] == '?':
63-
ahead = ' ' if i == 0 else s[i - 1]
64-
behind = ' ' if i == len(s) - 1 else s[i + 1]
65-
for c in string.ascii_lowercase:
66-
if c != ahead and c != behind:
67-
s[i] = c
68-
break
69-
return "".join(s)
60+
ans = list(s)
61+
for i, c in enumerate(ans):
62+
if c == '?':
63+
for cc in 'abc':
64+
if i > 0 and ans[i - 1] == cc:
65+
continue
66+
if i < len(s) - 1 and ans[i + 1] == cc:
67+
continue
68+
ans[i] = cc
69+
break
70+
return ''.join(ans)
7071
```
7172

7273
### **Java**
@@ -75,40 +76,70 @@ class Solution:
7576
class Solution {
7677
public String modifyString(String s) {
7778
char[] chars = s.toCharArray();
78-
for (int i = 0; i < chars.length; i++) {
79-
if (chars[i] == '?') {
80-
// 前面的字符
81-
char ahead = i == 0 ? ' ' : chars[i - 1];
82-
// 后面的字符
83-
char behind = i == chars.length - 1 ? ' ' : chars[i + 1];
84-
char temp = 'a';
85-
while (temp == ahead || temp == behind) {
86-
temp++;
79+
for (int i = 0; i < chars.length; ++i) {
80+
char c = chars[i];
81+
if (c == '?') {
82+
for (char cc = 'a'; cc <= 'c'; ++cc) {
83+
if (i > 0 && chars[i - 1] == cc) {
84+
continue;
85+
}
86+
if (i < chars.length - 1 && chars[i + 1] == cc) {
87+
continue;
88+
}
89+
chars[i] = cc;
90+
break;
8791
}
88-
chars[i] = temp;
8992
}
9093
}
91-
return new String(chars);
94+
return String.valueOf(chars);
9295
}
9396
}
9497
```
9598

99+
### **C++**
100+
101+
```cpp
102+
class Solution {
103+
public:
104+
string modifyString(string s) {
105+
for (int i = 0; i < s.size(); ++i)
106+
{
107+
if (s[i] == '?')
108+
{
109+
for (char cc : "abc")
110+
{
111+
if (i > 0 && s[i - 1] == cc) continue;
112+
if (i < s.size() - 1 && s[i + 1] == cc) continue;
113+
s[i] = cc;
114+
break;
115+
}
116+
}
117+
}
118+
return s;
119+
}
120+
};
121+
```
122+
96123
### **Go**
97124
98125
```go
99126
func modifyString(s string) string {
100-
data := []byte(" " + s + " ")
101-
for i, c := range data {
102-
if c == byte('?') {
103-
ahead, behind := data[i-1], data[i+1]
104-
for t := byte('a'); t <= byte('z'); t++ {
105-
if t != ahead && t != behind {
106-
data[i] = t
127+
ans := []byte(s)
128+
for i, c := range ans {
129+
if c == '?' {
130+
for cc := byte('a'); cc <= 'c'; cc++ {
131+
if i > 0 && ans[i-1] == cc {
132+
continue
133+
}
134+
if i < len(s)-1 && ans[i+1] == cc {
135+
continue
107136
}
137+
ans[i] = cc
138+
break
108139
}
109140
}
110141
}
111-
return string(data[1 : len(data)-1])
142+
return string(ans)
112143
}
113144
```
114145

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
string modifyString(string s) {
4+
for (int i = 0; i < s.size(); ++i)
5+
{
6+
if (s[i] == '?')
7+
{
8+
for (char cc : "abc")
9+
{
10+
if (i > 0 && s[i - 1] == cc) continue;
11+
if (i < s.size() - 1 && s[i + 1] == cc) continue;
12+
s[i] = cc;
13+
break;
14+
}
15+
}
16+
}
17+
return s;
18+
}
19+
};
Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
func modifyString(s string) string {
2-
data := []byte(" " + s + " ")
3-
for i, c := range data {
4-
if c == byte('?') {
5-
ahead, behind := data[i-1], data[i+1]
6-
for t := byte('a'); t <= byte('z'); t++ {
7-
if t != ahead && t != behind {
8-
data[i] = t
2+
ans := []byte(s)
3+
for i, c := range ans {
4+
if c == '?' {
5+
for cc := byte('a'); cc <= 'c'; cc++ {
6+
if i > 0 && ans[i-1] == cc {
7+
continue
98
}
9+
if i < len(s)-1 && ans[i+1] == cc {
10+
continue
11+
}
12+
ans[i] = cc
13+
break
1014
}
1115
}
1216
}
13-
return string(data[1 : len(data)-1])
17+
return string(ans)
1418
}
Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
class Solution {
22
public String modifyString(String s) {
33
char[] chars = s.toCharArray();
4-
for (int i = 0; i < chars.length; i++) {
5-
if (chars[i] == '?') {
6-
// 前面的字符
7-
char ahead = i == 0 ? ' ' : chars[i - 1];
8-
// 后面的字符
9-
char behind = i == chars.length - 1 ? ' ' : chars[i + 1];
10-
char temp = 'a';
11-
while (temp == ahead || temp == behind) {
12-
temp++;
4+
for (int i = 0; i < chars.length; ++i) {
5+
char c = chars[i];
6+
if (c == '?') {
7+
for (char cc = 'a'; cc <= 'c'; ++cc) {
8+
if (i > 0 && chars[i - 1] == cc) {
9+
continue;
10+
}
11+
if (i < chars.length - 1 && chars[i + 1] == cc) {
12+
continue;
13+
}
14+
chars[i] = cc;
15+
break;
1316
}
14-
chars[i] = temp;
1517
}
1618
}
17-
return new String(chars);
19+
return String.valueOf(chars);
1820
}
1921
}
Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
class Solution:
22
def modifyString(self, s: str) -> str:
3-
s = list(s)
4-
for i in range(len(s)):
5-
if s[i] == '?':
6-
ahead = ' ' if i == 0 else s[i - 1]
7-
behind = ' ' if i == len(s) - 1 else s[i + 1]
8-
for c in string.ascii_lowercase:
9-
if c != ahead and c != behind:
10-
s[i] = c
11-
break
12-
return "".join(s)
3+
ans = list(s)
4+
for i, c in enumerate(ans):
5+
if c == '?':
6+
for cc in 'abc':
7+
if i > 0 and ans[i - 1] == cc:
8+
continue
9+
if i < len(s) - 1 and ans[i + 1] == cc:
10+
continue
11+
ans[i] = cc
12+
break
13+
return ''.join(ans)

0 commit comments

Comments
 (0)