Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit de24412

Browse filesBrowse files
committed
feat: add solutions to lc problem: No.0723
No.0723.Candy Crush
1 parent 2ef5aca commit de24412
Copy full SHA for de24412

File tree

Expand file treeCollapse file tree

6 files changed

+530
-23
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

6 files changed

+530
-23
lines changed
Open diff view settings
Collapse file

‎solution/0700-0799/0723.Candy Crush/README.md‎

Copy file name to clipboardExpand all lines: solution/0700-0799/0723.Candy Crush/README.md
+181-2Lines changed: 181 additions & 2 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -44,27 +44,206 @@ board =
4444
<li>每个&nbsp;<code>board[i][j]</code>&nbsp;初始整数范围是&nbsp;[1, 2000]。</li>
4545
</ol>
4646

47-
4847
## 解法
4948

5049
<!-- 这里可写通用的实现逻辑 -->
5150

51+
循环消除,先标记每一行需要消除的元素,再标记每一列需要消除的元素(使用元素的负相反数进行标记),然后执行消除。
52+
5253
<!-- tabs:start -->
5354

5455
### **Python3**
5556

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

5859
```python
59-
60+
class Solution:
61+
def candyCrush(self, board: List[List[int]]) -> List[List[int]]:
62+
m, n = len(board), len(board[0])
63+
run = True
64+
while run:
65+
run = False
66+
for i in range(m):
67+
for j in range(n - 2):
68+
if board[i][j] != 0 and abs(board[i][j]) == abs(board[i][j + 1]) and abs(board[i][j]) == abs(board[i][j + 2]):
69+
run = True
70+
board[i][j] = board[i][j + 1] = board[i][j + 2] = -abs(board[i][j])
71+
for j in range(n):
72+
for i in range(m - 2):
73+
if board[i][j] != 0 and abs(board[i][j]) == abs(board[i + 1][j]) and abs(board[i][j]) == abs(board[i + 2][j]):
74+
run = True
75+
board[i][j] = board[i + 1][j] = board[i + 2][j] = -abs(board[i][j])
76+
if run:
77+
for j in range(n):
78+
curr = m - 1
79+
for i in range(m - 1, -1, -1):
80+
if board[i][j] > 0:
81+
board[curr][j] = board[i][j]
82+
curr -= 1
83+
while curr > -1:
84+
board[curr][j] = 0
85+
curr -= 1
86+
return board
6087
```
6188

6289
### **Java**
6390

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

6693
```java
94+
class Solution {
95+
public int[][] candyCrush(int[][] board) {
96+
int m = board.length, n = board[0].length;
97+
boolean run = true;
98+
while (run) {
99+
run = false;
100+
for (int i = 0; i < m; ++i) {
101+
for (int j = 0; j < n - 2; ++j) {
102+
if (board[i][j] != 0 && Math.abs(board[i][j]) == Math.abs(board[i][j + 1]) && Math.abs(board[i][j]) == Math.abs(board[i][j + 2])) {
103+
run = true;
104+
board[i][j] = board[i][j + 1] = board[i][j + 2] = -Math.abs(board[i][j]);
105+
}
106+
}
107+
}
108+
for (int j = 0; j < n; ++j) {
109+
for (int i = 0; i < m - 2; ++i) {
110+
if (board[i][j] != 0 && Math.abs(board[i][j]) == Math.abs(board[i + 1][j]) && Math.abs(board[i][j]) == Math.abs(board[i + 2][j])) {
111+
run = true;
112+
board[i][j] = board[i + 1][j] = board[i + 2][j] = -Math.abs(board[i][j]);
113+
}
114+
}
115+
}
116+
if (run) {
117+
for (int j = 0; j < n; ++j) {
118+
int curr = m - 1;
119+
for (int i = m - 1; i >= 0; --i) {
120+
if (board[i][j] > 0) {
121+
board[curr][j] = board[i][j];
122+
--curr;
123+
}
124+
}
125+
while (curr > -1) {
126+
board[curr][j] = 0;
127+
--curr;
128+
}
129+
}
130+
}
131+
}
132+
return board;
133+
}
134+
}
135+
```
136+
137+
### **C++**
138+
139+
```cpp
140+
class Solution {
141+
public:
142+
vector<vector<int>> candyCrush(vector<vector<int>>& board) {
143+
int m = board.size(), n = board[0].size();
144+
bool run = true;
145+
while (run)
146+
{
147+
run = false;
148+
for (int i = 0; i < m; ++i)
149+
{
150+
for (int j = 0; j < n - 2; ++j)
151+
{
152+
if (board[i][j] != 0 && abs(board[i][j]) == abs(board[i][j + 1]) && abs(board[i][j]) == abs(board[i][j + 2]))
153+
{
154+
run = true;
155+
board[i][j] = board[i][j + 1] = board[i][j + 2] = -abs(board[i][j]);
156+
}
157+
}
158+
}
159+
for (int j = 0; j < n; ++j)
160+
{
161+
for (int i = 0; i < m - 2; ++i)
162+
{
163+
if (board[i][j] != 0 && abs(board[i][j]) == abs(board[i + 1][j]) && abs(board[i][j]) == abs(board[i + 2][j]))
164+
{
165+
run = true;
166+
board[i][j] = board[i + 1][j] = board[i + 2][j] = -abs(board[i][j]);
167+
}
168+
}
169+
}
170+
if (run)
171+
{
172+
for (int j = 0; j < n; ++j)
173+
{
174+
int curr = m - 1;
175+
for (int i = m - 1; i >= 0; --i)
176+
{
177+
if (board[i][j] > 0)
178+
{
179+
board[curr][j] = board[i][j];
180+
--curr;
181+
}
182+
}
183+
while (curr > -1)
184+
{
185+
board[curr][j] = 0;
186+
--curr;
187+
}
188+
}
189+
}
190+
}
191+
return board;
192+
}
193+
};
194+
```
67195
196+
### **Go**
197+
198+
```go
199+
func candyCrush(board [][]int) [][]int {
200+
m, n := len(board), len(board[0])
201+
run := true
202+
for run {
203+
run = false
204+
for i := 0; i < m; i++ {
205+
for j := 0; j < n-2; j++ {
206+
if board[i][j] != 0 && abs(board[i][j]) == abs(board[i][j+1]) && abs(board[i][j]) == abs(board[i][j+2]) {
207+
run = true
208+
t := -abs(board[i][j])
209+
board[i][j], board[i][j+1], board[i][j+2] = t, t, t
210+
}
211+
}
212+
}
213+
for j := 0; j < n; j++ {
214+
for i := 0; i < m-2; i++ {
215+
if board[i][j] != 0 && abs(board[i][j]) == abs(board[i+1][j]) && abs(board[i][j]) == abs(board[i+2][j]) {
216+
run = true
217+
t := -abs(board[i][j])
218+
board[i][j], board[i+1][j], board[i+2][j] = t, t, t
219+
}
220+
}
221+
}
222+
if run {
223+
for j := 0; j < n; j++ {
224+
curr := m - 1
225+
for i := m - 1; i >= 0; i-- {
226+
if board[i][j] > 0 {
227+
board[curr][j] = board[i][j]
228+
curr--
229+
}
230+
}
231+
for curr > -1 {
232+
board[curr][j] = 0
233+
curr--
234+
}
235+
}
236+
}
237+
}
238+
return board
239+
}
240+
241+
func abs(x int) int {
242+
if x >= 0 {
243+
return x
244+
}
245+
return -x
246+
}
68247
```
69248

70249
### **...**

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.