Skip to content

Commit 23ce835

Browse files
authored
feat: add solutions to lc problems: No.1957,1958
* No.1957.Delete Characters to Make Fancy String * No.1958.Check if Move is Legal
1 parent 9a14a15 commit 23ce835

File tree

12 files changed

+424
-4
lines changed

12 files changed

+424
-4
lines changed

solution/1900-1999/1957.Delete Characters to Make Fancy String/README.md

+54-1
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,68 @@
6363
<!-- 这里可写当前语言的特殊实现逻辑 -->
6464

6565
```python
66-
66+
class Solution:
67+
def makeFancyString(self, s: str) -> str:
68+
ans = []
69+
for c in s:
70+
if len(ans) > 1 and ans[-1] == ans[-2] == c:
71+
continue
72+
ans.append(c)
73+
return ''.join(ans)
6774
```
6875

6976
### **Java**
7077

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

7380
```java
81+
class Solution {
82+
public String makeFancyString(String s) {
83+
StringBuilder ans = new StringBuilder();
84+
for (char c : s.toCharArray()) {
85+
int n = ans.length();
86+
if (n > 1 && ans.charAt(n - 1) == c && ans.charAt(n - 2) == c) {
87+
continue;
88+
}
89+
ans.append(c);
90+
}
91+
return ans.toString();
92+
}
93+
}
94+
```
95+
96+
### **C++**
97+
98+
```cpp
99+
class Solution {
100+
public:
101+
string makeFancyString(string s) {
102+
string ans = "";
103+
for (char& c : s)
104+
{
105+
int n = ans.size();
106+
if (n > 1 && ans[n - 1] == c && ans[n - 2] == c) continue;
107+
ans.push_back(c);
108+
}
109+
return ans;
110+
}
111+
};
112+
```
74113
114+
### **Go**
115+
116+
```go
117+
func makeFancyString(s string) string {
118+
ans := []rune{}
119+
for _, c := range s {
120+
n := len(ans)
121+
if n > 1 && ans[n-1] == c && ans[n-2] == c {
122+
continue
123+
}
124+
ans = append(ans, c)
125+
}
126+
return string(ans)
127+
}
75128
```
76129

77130
### **...**

solution/1900-1999/1957.Delete Characters to Make Fancy String/README_EN.md

+54-1
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,66 @@ No three consecutive characters are equal, so return &quot;aabaa&quot;.
5555
### **Python3**
5656

5757
```python
58-
58+
class Solution:
59+
def makeFancyString(self, s: str) -> str:
60+
ans = []
61+
for c in s:
62+
if len(ans) > 1 and ans[-1] == ans[-2] == c:
63+
continue
64+
ans.append(c)
65+
return ''.join(ans)
5966
```
6067

6168
### **Java**
6269

6370
```java
71+
class Solution {
72+
public String makeFancyString(String s) {
73+
StringBuilder ans = new StringBuilder();
74+
for (char c : s.toCharArray()) {
75+
int n = ans.length();
76+
if (n > 1 && ans.charAt(n - 1) == c && ans.charAt(n - 2) == c) {
77+
continue;
78+
}
79+
ans.append(c);
80+
}
81+
return ans.toString();
82+
}
83+
}
84+
```
85+
86+
### **C++**
87+
88+
```cpp
89+
class Solution {
90+
public:
91+
string makeFancyString(string s) {
92+
string ans = "";
93+
for (char& c : s)
94+
{
95+
int n = ans.size();
96+
if (n > 1 && ans[n - 1] == c && ans[n - 2] == c) continue;
97+
ans.push_back(c);
98+
}
99+
return ans;
100+
}
101+
};
102+
```
64103
104+
### **Go**
105+
106+
```go
107+
func makeFancyString(s string) string {
108+
ans := []rune{}
109+
for _, c := range s {
110+
n := len(ans)
111+
if n > 1 && ans[n-1] == c && ans[n-2] == c {
112+
continue
113+
}
114+
ans = append(ans, c)
115+
}
116+
return string(ans)
117+
}
65118
```
66119

67120
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
string makeFancyString(string s) {
4+
string ans = "";
5+
for (char& c : s)
6+
{
7+
int n = ans.size();
8+
if (n > 1 && ans[n - 1] == c && ans[n - 2] == c) continue;
9+
ans.push_back(c);
10+
}
11+
return ans;
12+
}
13+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
func makeFancyString(s string) string {
2+
ans := []rune{}
3+
for _, c := range s {
4+
n := len(ans)
5+
if n > 1 && ans[n-1] == c && ans[n-2] == c {
6+
continue
7+
}
8+
ans = append(ans, c)
9+
}
10+
return string(ans)
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public String makeFancyString(String s) {
3+
StringBuilder ans = new StringBuilder();
4+
for (char c : s.toCharArray()) {
5+
int n = ans.length();
6+
if (n > 1 && ans.charAt(n - 1) == c && ans.charAt(n - 2) == c) {
7+
continue;
8+
}
9+
ans.append(c);
10+
}
11+
return ans.toString();
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def makeFancyString(self, s: str) -> str:
3+
ans = []
4+
for c in s:
5+
if len(ans) > 1 and ans[-1] == ans[-2] == c:
6+
continue
7+
ans.append(c)
8+
return ''.join(ans)

solution/1900-1999/1958.Check if Move is Legal/README.md

+93-1
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,107 @@
5959
<!-- 这里可写当前语言的特殊实现逻辑 -->
6060

6161
```python
62-
62+
class Solution:
63+
def checkMove(self, board: List[List[str]], rMove: int, cMove: int, color: str) -> bool:
64+
dirs = [(1, 0), (0, 1), (-1, 0), (0, -1),
65+
(1, 1), (1, -1), (-1, 1), (-1, -1)]
66+
n = 8
67+
for a, b in dirs:
68+
i, j = rMove, cMove
69+
t = 0
70+
while 0 <= i + a < n and 0 <= j + b < n:
71+
t += 1
72+
i, j = i + a, j + b
73+
if board[i][j] in ['.', color]:
74+
break
75+
if board[i][j] == color and t > 1:
76+
return True
77+
return False
6378
```
6479

6580
### **Java**
6681

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

6984
```java
85+
class Solution {
86+
private static final int[][] DIRS = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
87+
private static final int N = 8;
88+
89+
public boolean checkMove(char[][] board, int rMove, int cMove, char color) {
90+
for (int[] d : DIRS) {
91+
int i = rMove, j = cMove;
92+
int t = 0;
93+
int a = d[0], b = d[1];
94+
while (0 <= i + a && i + a < N && 0 <= j + b && j + b < N) {
95+
++t;
96+
i += a;
97+
j += b;
98+
if (board[i][j] == '.' || board[i][j] == color) {
99+
break;
100+
}
101+
}
102+
if (board[i][j] == color && t > 1) {
103+
return true;
104+
}
105+
}
106+
return false;
107+
}
108+
}
109+
```
110+
111+
### **C++**
112+
113+
```cpp
114+
class Solution {
115+
public:
116+
vector<vector<int>> dirs = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
117+
int n = 8;
118+
119+
bool checkMove(vector<vector<char>>& board, int rMove, int cMove, char color) {
120+
for (auto& d : dirs)
121+
{
122+
int a = d[0], b = d[1];
123+
int i = rMove, j = cMove;
124+
int t = 0;
125+
while (0 <= i + a && i + a < n && 0 <= j + b && j + b < n)
126+
{
127+
++t;
128+
i += a;
129+
j += b;
130+
if (board[i][j] == '.' || board[i][j] == color) break;
131+
}
132+
if (board[i][j] == color && t > 1) return true;
133+
}
134+
return false;
135+
}
136+
};
137+
```
70138

139+
### **Go**
140+
141+
```go
142+
func checkMove(board [][]byte, rMove int, cMove int, color byte) bool {
143+
dirs := [8][2]int{{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}
144+
n := 8
145+
for _, d := range dirs {
146+
a, b := d[0], d[1]
147+
i, j := rMove, cMove
148+
t := 0
149+
for 0 <= i+a && i+a < n && 0 <= j+b && j+b < n {
150+
t++
151+
i += a
152+
j += b
153+
if board[i][j] == '.' || board[i][j] == color {
154+
break
155+
}
156+
}
157+
if board[i][j] == color && t > 1 {
158+
return true
159+
}
160+
}
161+
return false
162+
}
71163
```
72164

73165
### **...**

0 commit comments

Comments
 (0)