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

Sign up
Appearance settings

Commit 0d6cfe4

Browse files
feat: add solutions to lc problems: No.0723,1493,2730 (doocs#3324)
1 parent 583ebf7 commit 0d6cfe4

File tree

22 files changed

+893
-181
lines changed

22 files changed

+893
-181
lines changed

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

Lines changed: 121 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,11 @@ tags:
7171

7272
<!-- solution:start -->
7373

74-
### 方法一
74+
### 方法一:模拟
75+
76+
我们可以逐行和逐列遍历矩阵,找到连续三个相同的元素,将它们标记为负数。如果成功标记,我们需要将矩阵中的元素下移,直到没有元素可以下移为止。
77+
78+
时间复杂度 $O(m^2 \times n^2),ドル其中 $m$ 和 $n$ 分别是矩阵的行数和列数。空间复杂度 $O(1)$。
7579

7680
<!-- tabs:start -->
7781

@@ -85,37 +89,33 @@ class Solution:
8589
while run:
8690
run = False
8791
for i in range(m):
88-
for j in range(n - 2):
89-
if (
90-
board[i][j] != 0
91-
and abs(board[i][j]) == abs(board[i][j + 1])
92-
and abs(board[i][j]) == abs(board[i][j + 2])
92+
for j in range(2, n):
93+
if board[i][j] and abs(board[i][j]) == abs(board[i][j - 1]) == abs(
94+
board[i][j - 2]
9395
):
9496
run = True
95-
board[i][j] = board[i][j + 1] = board[i][j + 2] = -abs(
97+
board[i][j] = board[i][j - 1] = board[i][j - 2] = -abs(
9698
board[i][j]
9799
)
98100
for j in range(n):
99-
for i in range(m - 2):
100-
if (
101-
board[i][j] != 0
102-
and abs(board[i][j]) == abs(board[i + 1][j])
103-
and abs(board[i][j]) == abs(board[i + 2][j])
101+
for i in range(2, m):
102+
if board[i][j] and abs(board[i][j]) == abs(board[i - 1][j]) == abs(
103+
board[i - 2][j]
104104
):
105105
run = True
106-
board[i][j] = board[i + 1][j] = board[i + 2][j] = -abs(
106+
board[i][j] = board[i - 1][j] = board[i - 2][j] = -abs(
107107
board[i][j]
108108
)
109109
if run:
110110
for j in range(n):
111-
curr = m - 1
111+
k = m - 1
112112
for i in range(m - 1, -1, -1):
113113
if board[i][j] > 0:
114-
board[curr][j] = board[i][j]
115-
curr -= 1
116-
while curr >-1:
117-
board[curr][j] = 0
118-
curr -= 1
114+
board[k][j] = board[i][j]
115+
k -= 1
116+
while k >=0:
117+
board[k][j] = 0
118+
k -= 1
119119
return board
120120
```
121121

@@ -126,42 +126,46 @@ class Solution {
126126
public int[][] candyCrush(int[][] board) {
127127
int m = board.length, n = board[0].length;
128128
boolean run = true;
129+
129130
while (run) {
130131
run = false;
131-
for (int i = 0; i < m; ++i) {
132-
for (int j = 0; j < n-2; ++j) {
133-
if (board[i][j] != 0 && Math.abs(board[i][j]) == Math.abs(board[i][j + 1])
134-
&& Math.abs(board[i][j]) == Math.abs(board[i][j + 2])) {
132+
for (int i = 0; i < m; i++) {
133+
for (int j = 2; j < n; j++) {
134+
if (board[i][j] != 0 && Math.abs(board[i][j]) == Math.abs(board[i][j - 1])
135+
&& Math.abs(board[i][j]) == Math.abs(board[i][j - 2])) {
135136
run = true;
136-
board[i][j] = board[i][j + 1] = board[i][j + 2] = -Math.abs(board[i][j]);
137+
int val = Math.abs(board[i][j]);
138+
board[i][j] = board[i][j - 1] = board[i][j - 2] = -val;
137139
}
138140
}
139141
}
140-
for (int j = 0; j < n; ++j) {
141-
for (int i = 0; i < m-2; ++i) {
142-
if (board[i][j] != 0 && Math.abs(board[i][j]) == Math.abs(board[i + 1][j])
143-
&& Math.abs(board[i][j]) == Math.abs(board[i + 2][j])) {
142+
for (int j = 0; j < n; j++) {
143+
for (int i = 2; i < m; i++) {
144+
if (board[i][j] != 0 && Math.abs(board[i][j]) == Math.abs(board[i - 1][j])
145+
&& Math.abs(board[i][j]) == Math.abs(board[i - 2][j])) {
144146
run = true;
145-
board[i][j] = board[i + 1][j] = board[i + 2][j] = -Math.abs(board[i][j]);
147+
int val = Math.abs(board[i][j]);
148+
board[i][j] = board[i - 1][j] = board[i - 2][j] = -val;
146149
}
147150
}
148151
}
149152
if (run) {
150-
for (int j = 0; j < n; ++j) {
151-
int curr = m - 1;
152-
for (int i = m - 1; i >= 0; --i) {
153+
for (int j = 0; j < n; j++) {
154+
int k = m - 1;
155+
for (int i = m - 1; i >= 0; i--) {
153156
if (board[i][j] > 0) {
154-
board[curr][j] = board[i][j];
155-
--curr;
157+
board[k][j] = board[i][j];
158+
k--;
156159
}
157160
}
158-
while (curr >-1) {
159-
board[curr][j] = 0;
160-
--curr;
161+
while (k >=0) {
162+
board[k][j] = 0;
163+
k--;
161164
}
162165
}
163166
}
164167
}
168+
165169
return board;
166170
}
167171
}
@@ -218,52 +222,114 @@ public:
218222
219223
```go
220224
func candyCrush(board [][]int) [][]int {
221-
m, n := len(board), len(board[0])
225+
m := len(board)
226+
n := len(board[0])
222227
run := true
228+
223229
for run {
224230
run = false
225231
for i := 0; i < m; i++ {
226-
for j := 0; j < n-2; j++ {
227-
if board[i][j] != 0 && abs(board[i][j]) == abs(board[i][j+1]) && abs(board[i][j]) == abs(board[i][j+2]) {
232+
for j := 2; j < n; j++ {
233+
if board[i][j] != 0 && abs(board[i][j]) == abs(board[i][j-1]) && abs(board[i][j]) == abs(board[i][j-2]) {
228234
run = true
229-
t := -abs(board[i][j])
230-
board[i][j], board[i][j+1], board[i][j+2] = t, t, t
235+
val := abs(board[i][j])
236+
board[i][j] = -val
237+
board[i][j-1] = -val
238+
board[i][j-2] = -val
231239
}
232240
}
233241
}
234242
for j := 0; j < n; j++ {
235-
for i := 0; i < m-2; i++ {
236-
if board[i][j] != 0 && abs(board[i][j]) == abs(board[i+1][j]) && abs(board[i][j]) == abs(board[i+2][j]) {
243+
for i := 2; i < m; i++ {
244+
if board[i][j] != 0 && abs(board[i][j]) == abs(board[i-1][j]) && abs(board[i][j]) == abs(board[i-2][j]) {
237245
run = true
238-
t := -abs(board[i][j])
239-
board[i][j], board[i+1][j], board[i+2][j] = t, t, t
246+
val := abs(board[i][j])
247+
board[i][j] = -val
248+
board[i-1][j] = -val
249+
board[i-2][j] = -val
240250
}
241251
}
242252
}
243253
if run {
244254
for j := 0; j < n; j++ {
245-
curr := m - 1
255+
k := m - 1
246256
for i := m - 1; i >= 0; i-- {
247257
if board[i][j] > 0 {
248-
board[curr][j] = board[i][j]
249-
curr--
258+
board[k][j] = board[i][j]
259+
k--
250260
}
251261
}
252-
for curr > -1 {
253-
board[curr][j] = 0
254-
curr--
262+
for k >= 0 {
263+
board[k][j] = 0
264+
k--
255265
}
256266
}
257267
}
258268
}
269+
259270
return board
260271
}
261272
262273
func abs(x int) int {
263-
if x >= 0 {
264-
return x
274+
if x < 0 {
275+
return -x
265276
}
266-
return -x
277+
return x
278+
}
279+
```
280+
281+
#### TypeScript
282+
283+
```ts
284+
function candyCrush(board: number[][]): number[][] {
285+
const m = board.length;
286+
const n = board[0].length;
287+
let run = true;
288+
while (run) {
289+
run = false;
290+
for (let i = 0; i < m; i++) {
291+
for (let j = 2; j < n; j++) {
292+
if (
293+
board[i][j] !== 0 &&
294+
Math.abs(board[i][j]) === Math.abs(board[i][j - 1]) &&
295+
Math.abs(board[i][j]) === Math.abs(board[i][j - 2])
296+
) {
297+
run = true;
298+
const val = Math.abs(board[i][j]);
299+
board[i][j] = board[i][j - 1] = board[i][j - 2] = -val;
300+
}
301+
}
302+
}
303+
for (let j = 0; j < n; j++) {
304+
for (let i = 2; i < m; i++) {
305+
if (
306+
board[i][j] !== 0 &&
307+
Math.abs(board[i][j]) === Math.abs(board[i - 1][j]) &&
308+
Math.abs(board[i][j]) === Math.abs(board[i - 2][j])
309+
) {
310+
run = true;
311+
const val = Math.abs(board[i][j]);
312+
board[i][j] = board[i - 1][j] = board[i - 2][j] = -val;
313+
}
314+
}
315+
}
316+
if (run) {
317+
for (let j = 0; j < n; j++) {
318+
let k = m - 1;
319+
for (let i = m - 1; i >= 0; i--) {
320+
if (board[i][j] > 0) {
321+
board[k][j] = board[i][j];
322+
k--;
323+
}
324+
}
325+
while (k >= 0) {
326+
board[k][j] = 0;
327+
k--;
328+
}
329+
}
330+
}
331+
}
332+
return board;
267333
}
268334
```
269335

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /