Skip to content

Commit 1185cab

Browse files
committed
add: Surrounded Regions
1 parent 2bb3ecc commit 1185cab

File tree

3 files changed

+155
-0
lines changed

3 files changed

+155
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
2727
|69|[Sqrt(x)](https://leetcode.com/problems/sqrtx/) | [JavaScript](./src/sqrtx/res.js)|Easy|
2828
|73|[Set Matrix Zeroes](https://leetcode.com/problems/set-matrix-zeroes/) | [JavaScript](./src/set-matrix-zeroes/res.js)|Medium|
2929
|75|[Sort Colors](https://leetcode.com/problems/sort-colors/) | [JavaScript](./src/sort-colors/res.js)|Medium|
30+
|130|[Surrounded Regions](https://leetcode.com/problems/surrounded-regions/) | [JavaScript](./src/surrounded-regions/res.js)|Medium|
3031
|136|[Single Number](https://leetcode.com/problems/single-number/) | [JavaScript](./src/single-number/res.js)|Easy|
3132
|175|[Combine Two Tables](https://leetcode.com/problems/combine-two-tables/)| [SQL](./src/combine-two-tables/res.txt)|Easy|
3233
|176|[Second Highest Salary](https://leetcode.com/problems/second-highest-salary/)| [SQL](./src/second-highest-salary/res.txt)|Easy|

src/surrounded-regions/res.js

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/**
2+
* res.js
3+
* @authors Joe Jiang (hijiangtao@gmail.com)
4+
* @date 2017-03-10 22:17:27
5+
* @version $Id$
6+
*
7+
* @param {character[][]} board
8+
* @return {void} Do not return anything, modify board in-place instead.
9+
*/
10+
11+
let solve = function(board) {
12+
if (board.length === 0) {
13+
return ;
14+
}
15+
16+
let rowlen = board.length,
17+
collen = board[0].length,
18+
rowecp = rowlen-1,
19+
colecp = collen-1;
20+
21+
if (rowlen <= 2 || collen <= 2) {
22+
return ;
23+
}
24+
25+
for (let i=0; i<collen; i++) {
26+
// 判断第一行是否存在'O'
27+
if (board[0][i] === 'O') {
28+
board[0][i] = '1';
29+
if (board[1][i] === 'O' && i!==0 && i!==collen-1) {
30+
judZero(1, i, rowlen, collen, board);
31+
}
32+
}
33+
// judZero(0, i, rowlen, collen, board);
34+
35+
// 判断最后一行是否存在'O'
36+
if (board[rowecp][i] === 'O') {
37+
board[rowecp][i] = '1';
38+
if (board[rowecp-1][i] === 'O' && i!==0 && i!==collen-1) {
39+
judZero(rowecp-1, i, rowlen, collen, board);
40+
}
41+
}
42+
43+
// judZero(rowecp, i, rowlen, collen, board);
44+
}
45+
46+
47+
for (let i=1; i<rowecp; i++) {
48+
// 判断第一列是否存在'O'
49+
if (board[i][0] === 'O') {
50+
board[i][0] = '1';
51+
if (board[i][1] === 'O' && i!==0 && i!==rowecp) {
52+
judZero(i, 1, rowlen, collen, board);
53+
}
54+
}
55+
56+
// judZero(i, 0, rowlen, collen, board);
57+
58+
// 判断最后一列是否存在'O'
59+
if (board[i][colecp] === 'O') {
60+
board[i][colecp] = '1';
61+
if (board[i][colecp-1] === 'O' && i!==0 && i!==rowecp) {
62+
judZero(i, colecp-1, rowlen, collen, board);
63+
}
64+
}
65+
66+
// judZero(i, colecp, rowlen, collen, board);
67+
}
68+
69+
for (let i=0; i<rowlen; i++) {
70+
for (let j=0; j<collen; j++) {
71+
if (board[i][j] === '1') {
72+
board[i][j] = 'O';
73+
} else if (board[i][j] === 'O') {
74+
board[i][j] = 'X';
75+
}
76+
}
77+
}
78+
79+
return ;
80+
};
81+
82+
let judZero = function(x, y, rowlen, collen, matrix) {
83+
// if (matrix[x][y] !== 'O') {
84+
// return ;
85+
// }
86+
87+
matrix[x][y] = '1';
88+
89+
if (x-1 > 1 && matrix[x-1][y] === 'O') {
90+
judZero(x-1, y, rowlen, collen, matrix);
91+
}
92+
if (x+1 < rowlen-2 && matrix[x+1][y] === 'O') {
93+
judZero(x+1, y, rowlen, collen, matrix);
94+
}
95+
if (y-1 > 1 && matrix[x][y-1] === 'O') {
96+
judZero(x, y-1, rowlen, collen, matrix);
97+
}
98+
if (y+1 < collen-2 && matrix[x][y+1] === 'O') {
99+
judZero(x, y+1, rowlen, collen, matrix);
100+
}
101+
};
102+
103+

src/surrounded-regions/res.py

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# @Date : 2017-03-10 22:33:36
4+
# @Author : Joe Jiang (hijiangtao@gmail.com)
5+
# @Link : https://hijiangtao.github.io/
6+
# @Version : $Id$
7+
8+
class Solution(object):
9+
def solve(self, board):
10+
"""
11+
:type board: List[List[str]]
12+
:rtype: void Do not return anything, modify board in-place instead.
13+
"""
14+
15+
if len(board) == 0:
16+
return 0
17+
18+
rowlen = len(board)
19+
collen = len(board[0])
20+
rowecp = rowlen-1
21+
colecp = collen-1
22+
23+
for i in xrange(0,collen):
24+
self.judZero(0, i, rowlen, collen, board)
25+
self.judZero(rowecp, i, rowlen, collen, board)
26+
27+
for i in xrange(1,rowecp):
28+
self.judZero(i, 0, rowlen, collen, board)
29+
self.judZero(i, colecp, rowlen, collen, board)
30+
31+
for x in xrange(0,rowlen):
32+
for y in xrange(0,collen):
33+
if board[x][y] == '1':
34+
board[x][y] = 'O'
35+
if board[x][y] == 'O':
36+
board[x][y] = 'X'
37+
38+
def judZero(self, x, y, rowlen, collen, matrix):
39+
if matrix[x][y] !== 'O':
40+
return 0
41+
42+
matrix[x][y] = '1'
43+
44+
if x-1 >= 0:
45+
self.judZero(x-1, y, rowlen, collen, matrix)
46+
if x+1 < rowlen:
47+
self.judZero(x+1, y, rowlen, collen, matrix)
48+
if y-1 >= 0:
49+
self.judZero(x, y-1, rowlen, collen, matrix)
50+
if y+1 < collen:
51+
self.judZero(x, y+1, rowlen, collen, matrix)

0 commit comments

Comments
 (0)