Skip to content

Commit 053d2be

Browse files
committed
feat: add solutions to lc problem: No.2038
No.2038.Remove Colored Pieces if Both Neighbors are the Same Color
1 parent 41e4b30 commit 053d2be

File tree

8 files changed

+812
-554
lines changed

8 files changed

+812
-554
lines changed

README.md

Lines changed: 284 additions & 284 deletions
Large diffs are not rendered by default.

README_EN.md

Lines changed: 268 additions & 268 deletions
Large diffs are not rendered by default.

solution/2000-2099/2038.Remove Colored Pieces if Both Neighbors are the Same Color/README.md

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,22 +75,112 @@ ABBBB<strong><em>B</em></strong>BBAA -&gt; ABBBBBBAA
7575

7676
<!-- 这里可写通用的实现逻辑 -->
7777

78+
统计字符串中连续出现 3 个 'A' 或 3 个 'B' 的个数,分别记为 cnt1, cnt2。只要 cnt1 大于 cnt2,返回 true,否则返回 false。
79+
7880
<!-- tabs:start -->
7981

8082
### **Python3**
8183

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

8486
```python
85-
87+
class Solution:
88+
def winnerOfGame(self, colors: str) -> bool:
89+
a = b = 0
90+
cnt1 = cnt2 = 0
91+
for c in colors:
92+
if c == 'A':
93+
a += 1
94+
if a > 2:
95+
cnt1 += 1
96+
b = 0
97+
else:
98+
b += 1
99+
if b > 2:
100+
cnt2 += 1
101+
a = 0
102+
return cnt1 > cnt2
86103
```
87104

88105
### **Java**
89106

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

92109
```java
110+
class Solution {
111+
public boolean winnerOfGame(String colors) {
112+
int a = 0, b = 0;
113+
int cnt1 = 0, cnt2 = 0;
114+
for (char c : colors.toCharArray()) {
115+
if (c == 'A') {
116+
++a;
117+
if (a > 2) {
118+
++cnt1;
119+
}
120+
b = 0;
121+
} else {
122+
++b;
123+
if (b > 2) {
124+
++cnt2;
125+
}
126+
a = 0;
127+
}
128+
}
129+
return cnt1 > cnt2;
130+
}
131+
}
132+
```
133+
134+
### **C++**
135+
136+
```cpp
137+
class Solution {
138+
public:
139+
bool winnerOfGame(string colors) {
140+
int a = 0, b = 0;
141+
int cnt1 = 0, cnt2 = 0;
142+
for (char& c : colors)
143+
{
144+
if (c == 'A')
145+
{
146+
++a;
147+
if (a > 2) ++cnt1;
148+
b = 0;
149+
}
150+
else
151+
{
152+
++b;
153+
if (b > 2) ++cnt2;
154+
a = 0;
155+
}
156+
}
157+
return cnt1 > cnt2;
158+
}
159+
};
160+
```
93161
162+
### **Go**
163+
164+
```go
165+
func winnerOfGame(colors string) bool {
166+
var a, b, cnt1, cnt2 int
167+
for _, c := range colors {
168+
if c == 'A' {
169+
a++
170+
if a > 2 {
171+
cnt1++
172+
}
173+
b = 0
174+
} else {
175+
b++
176+
if b > 2 {
177+
cnt2++
178+
}
179+
a = 0
180+
}
181+
}
182+
return cnt1 > cnt2
183+
}
94184
```
95185

96186
### **...**

solution/2000-2099/2038.Remove Colored Pieces if Both Neighbors are the Same Color/README_EN.md

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,101 @@ Thus, Bob wins, so return false.
7777
### **Python3**
7878

7979
```python
80-
80+
class Solution:
81+
def winnerOfGame(self, colors: str) -> bool:
82+
a = b = 0
83+
cnt1 = cnt2 = 0
84+
for c in colors:
85+
if c == 'A':
86+
a += 1
87+
if a > 2:
88+
cnt1 += 1
89+
b = 0
90+
else:
91+
b += 1
92+
if b > 2:
93+
cnt2 += 1
94+
a = 0
95+
return cnt1 > cnt2
8196
```
8297

8398
### **Java**
8499

85100
```java
101+
class Solution {
102+
public boolean winnerOfGame(String colors) {
103+
int a = 0, b = 0;
104+
int cnt1 = 0, cnt2 = 0;
105+
for (char c : colors.toCharArray()) {
106+
if (c == 'A') {
107+
++a;
108+
if (a > 2) {
109+
++cnt1;
110+
}
111+
b = 0;
112+
} else {
113+
++b;
114+
if (b > 2) {
115+
++cnt2;
116+
}
117+
a = 0;
118+
}
119+
}
120+
return cnt1 > cnt2;
121+
}
122+
}
123+
```
124+
125+
### **C++**
126+
127+
```cpp
128+
class Solution {
129+
public:
130+
bool winnerOfGame(string colors) {
131+
int a = 0, b = 0;
132+
int cnt1 = 0, cnt2 = 0;
133+
for (char& c : colors)
134+
{
135+
if (c == 'A')
136+
{
137+
++a;
138+
if (a > 2) ++cnt1;
139+
b = 0;
140+
}
141+
else
142+
{
143+
++b;
144+
if (b > 2) ++cnt2;
145+
a = 0;
146+
}
147+
}
148+
return cnt1 > cnt2;
149+
}
150+
};
151+
```
86152
153+
### **Go**
154+
155+
```go
156+
func winnerOfGame(colors string) bool {
157+
var a, b, cnt1, cnt2 int
158+
for _, c := range colors {
159+
if c == 'A' {
160+
a++
161+
if a > 2 {
162+
cnt1++
163+
}
164+
b = 0
165+
} else {
166+
b++
167+
if b > 2 {
168+
cnt2++
169+
}
170+
a = 0
171+
}
172+
}
173+
return cnt1 > cnt2
174+
}
87175
```
88176

89177
### **...**
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
bool winnerOfGame(string colors) {
4+
int a = 0, b = 0;
5+
int cnt1 = 0, cnt2 = 0;
6+
for (char& c : colors)
7+
{
8+
if (c == 'A')
9+
{
10+
++a;
11+
if (a > 2) ++cnt1;
12+
b = 0;
13+
}
14+
else
15+
{
16+
++b;
17+
if (b > 2) ++cnt2;
18+
a = 0;
19+
}
20+
}
21+
return cnt1 > cnt2;
22+
}
23+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
func winnerOfGame(colors string) bool {
2+
var a, b, cnt1, cnt2 int
3+
for _, c := range colors {
4+
if c == 'A' {
5+
a++
6+
if a > 2 {
7+
cnt1++
8+
}
9+
b = 0
10+
} else {
11+
b++
12+
if b > 2 {
13+
cnt2++
14+
}
15+
a = 0
16+
}
17+
}
18+
return cnt1 > cnt2
19+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public boolean winnerOfGame(String colors) {
3+
int a = 0, b = 0;
4+
int cnt1 = 0, cnt2 = 0;
5+
for (char c : colors.toCharArray()) {
6+
if (c == 'A') {
7+
++a;
8+
if (a > 2) {
9+
++cnt1;
10+
}
11+
b = 0;
12+
} else {
13+
++b;
14+
if (b > 2) {
15+
++cnt2;
16+
}
17+
a = 0;
18+
}
19+
}
20+
return cnt1 > cnt2;
21+
}
22+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def winnerOfGame(self, colors: str) -> bool:
3+
a = b = 0
4+
cnt1 = cnt2 = 0
5+
for c in colors:
6+
if c == 'A':
7+
a += 1
8+
if a > 2:
9+
cnt1 += 1
10+
b = 0
11+
else:
12+
b += 1
13+
if b > 2:
14+
cnt2 += 1
15+
a = 0
16+
return cnt1 > cnt2

0 commit comments

Comments
 (0)