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 7b3594a

Browse files
committed
feat: add solutions to lc problem: No.1034
No.1034.Coloring A Border
1 parent a3715f8 commit 7b3594a

File tree

5 files changed

+322
-29
lines changed

5 files changed

+322
-29
lines changed

‎solution/1000-1099/1034.Coloring A Border/README.md‎

Lines changed: 121 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,27 +48,146 @@
4848

4949
<p>&nbsp;</p>
5050

51-
5251
## 解法
5352

5453
<!-- 这里可写通用的实现逻辑 -->
5554

55+
深度优先搜索,利用 vis 记录访问过的位置。
56+
5657
<!-- tabs:start -->
5758

5859
### **Python3**
5960

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

6263
```python
63-
64+
class Solution:
65+
def colorBorder(self, grid: List[List[int]], row: int, col: int, color: int) -> List[List[int]]:
66+
m, n = len(grid), len(grid[0])
67+
vis = [[False] * n for _ in range(m)]
68+
69+
def dfs(i, j, color):
70+
vis[i][j] = True
71+
old_color = grid[i][j]
72+
for a, b in [[-1, 0], [1, 0], [0, -1], [0, 1]]:
73+
x, y = a + i, b + j
74+
if 0 <= x < m and 0 <= y < n:
75+
if not vis[x][y]:
76+
if grid[x][y] == old_color:
77+
dfs(x, y, color)
78+
else:
79+
grid[i][j] = color
80+
else:
81+
grid[i][j] = color
82+
83+
dfs(row, col, color)
84+
return grid
6485
```
6586

6687
### **Java**
6788

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

7091
```java
92+
class Solution {
93+
private int[] dirs = new int[]{-1, 0, 1, 0, -1};
94+
95+
public int[][] colorBorder(int[][] grid, int r0, int c0, int color) {
96+
boolean[][] vis = new boolean[grid.length][grid[0].length];
97+
dfs(grid, r0, c0, color, vis);
98+
return grid;
99+
}
100+
101+
private void dfs(int[][] grid, int i, int j, int color, boolean[][] vis) {
102+
vis[i][j] = true;
103+
int oldColor = grid[i][j];
104+
for (int k = 0; k < 4; ++k) {
105+
int x = i + dirs[k], y = j + dirs[k + 1];
106+
if (x >= 0 && x < grid.length && y >= 0 && y < grid[0].length) {
107+
if (!vis[x][y]) {
108+
if (grid[x][y] == oldColor) {
109+
dfs(grid, x, y, color, vis);
110+
} else {
111+
grid[i][j] = color;
112+
}
113+
}
114+
} else {
115+
grid[i][j] = color;
116+
}
117+
}
118+
}
119+
}
120+
```
121+
122+
### **C++**
123+
124+
```cpp
125+
class Solution {
126+
public:
127+
int m, n;
128+
vector<vector<int>> dirs = {{0, 1}, {0, - 1}, {1, 0}, {-1, 0}};
129+
130+
vector<vector<int>> colorBorder(vector<vector<int>>& grid, int row, int col, int color) {
131+
m = grid.size();
132+
n = grid[0].size();
133+
vector<vector<bool>> vis(m, vector<bool>(n, false));
134+
dfs(row, col, color, grid, vis);
135+
return grid;
136+
}
137+
138+
void dfs(int i, int j, int color, vector<vector<int>>& grid, vector<vector<bool>>& vis) {
139+
vis[i][j] = true;
140+
int oldColor = grid[i][j];
141+
for (auto& dir : dirs)
142+
{
143+
int x = i + dir[0], y = j + dir[1];
144+
if (x >= 0 && x < m && y >= 0 && y < n)
145+
{
146+
if (!vis[x][y])
147+
{
148+
if (grid[x][y] == oldColor) dfs(x, y, color, grid, vis);
149+
else grid[i][j] = color;
150+
}
151+
}
152+
else grid[i][j] = color;
153+
}
154+
}
155+
};
156+
```
71157
158+
### **Go**
159+
160+
```go
161+
func colorBorder(grid [][]int, row int, col int, color int) [][]int {
162+
m, n := len(grid), len(grid[0])
163+
vis := make([][]bool, m)
164+
for i := 0; i < m; i++ {
165+
vis[i] = make([]bool, n)
166+
}
167+
dirs := [4][2]int{{0, -1}, {0, 1}, {1, 0}, {-1, 0}}
168+
169+
var dfs func(i, j, color int)
170+
dfs = func(i, j, color int) {
171+
vis[i][j] = true
172+
oldColor := grid[i][j]
173+
for _, dir := range dirs {
174+
x, y := i+dir[0], j+dir[1]
175+
if x >= 0 && x < m && y >= 0 && y < n {
176+
if !vis[x][y] {
177+
if grid[x][y] == oldColor {
178+
dfs(x, y, color)
179+
} else {
180+
grid[i][j] = color
181+
}
182+
}
183+
} else {
184+
grid[i][j] = color
185+
}
186+
}
187+
}
188+
dfs(row, col, color)
189+
return grid
190+
}
72191
```
73192

74193
### **...**

‎solution/1000-1099/1034.Coloring A Border/README_EN.md‎

Lines changed: 119 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,16 @@
66

77
<p>Given a 2-dimensional&nbsp;<code>grid</code> of integers, each value in the grid represents the color of the grid square at that location.</p>
88

9-
10-
119
<p>Two squares belong to the same <em>connected component</em> if and only if they have the same color and are next to each other in any of the 4 directions.</p>
1210

13-
14-
1511
<p>The&nbsp;<em>border</em> of a connected component is&nbsp;all the squares in the connected component that are&nbsp;either 4-directionally adjacent to&nbsp;a square not in the component, or on the boundary of the grid (the first or last row or column).</p>
1612

17-
18-
1913
<p>Given a square at location&nbsp;<code>(r0, c0)</code>&nbsp;in the grid and a <code>color</code>, color the&nbsp;border of the connected component of that square with the given <code>color</code>, and return the final <code>grid</code>.</p>
2014

21-
22-
2315
<p>&nbsp;</p>
2416

25-
26-
2717
<p><strong>Example 1:</strong></p>
2818

29-
30-
3119
<pre>
3220

3321
<strong>Input: </strong>grid = <span id="example-input-1-1">[[1,1],[1,2]]</span>, r0 = <span id="example-input-1-2">0</span>, c0 = <span id="example-input-1-3">0</span>, color = <span id="example-input-1-4">3</span>
@@ -36,14 +24,10 @@
3624

3725
</pre>
3826

39-
40-
4127
<div>
4228

4329
<p><strong>Example 2:</strong></p>
4430

45-
46-
4731
<pre>
4832

4933
<strong>Input: </strong>grid = <span id="example-input-2-1">[[1,2,2],[2,3,2]]</span>, r0 = <span id="example-input-2-2">0</span>, c0 = <span id="example-input-2-3">1</span>, color = <span id="example-input-2-4">3</span>
@@ -52,14 +36,10 @@
5236

5337
</pre>
5438

55-
56-
5739
<div>
5840

5941
<p><strong>Example 3:</strong></p>
6042

61-
62-
6343
<pre>
6444

6545
<strong>Input: </strong>grid = <span id="example-input-3-1">[[1,1,1],[1,1,1],[1,1,1]]</span>, r0 = <span id="example-input-3-2">1</span>, c0 = <span id="example-input-3-3">1</span>, color = <span id="example-input-3-4">2</span>
@@ -70,16 +50,10 @@
7050

7151
</div>
7252

73-
74-
7553
<p>&nbsp;</p>
7654

77-
78-
7955
<p><strong>Note:</strong></p>
8056

81-
82-
8357
<ol>
8458
<li><code>1 &lt;= grid.length &lt;= 50</code></li>
8559
<li><code>1 &lt;= grid[0].length &lt;= 50</code></li>
@@ -96,13 +70,131 @@
9670
### **Python3**
9771

9872
```python
99-
73+
class Solution:
74+
def colorBorder(self, grid: List[List[int]], row: int, col: int, color: int) -> List[List[int]]:
75+
m, n = len(grid), len(grid[0])
76+
vis = [[False] * n for _ in range(m)]
77+
78+
def dfs(i, j, color):
79+
vis[i][j] = True
80+
old_color = grid[i][j]
81+
for a, b in [[-1, 0], [1, 0], [0, -1], [0, 1]]:
82+
x, y = a + i, b + j
83+
if 0 <= x < m and 0 <= y < n:
84+
if not vis[x][y]:
85+
if grid[x][y] == old_color:
86+
dfs(x, y, color)
87+
else:
88+
grid[i][j] = color
89+
else:
90+
grid[i][j] = color
91+
92+
dfs(row, col, color)
93+
return grid
10094
```
10195

10296
### **Java**
10397

10498
```java
99+
class Solution {
100+
private int[] dirs = new int[]{-1, 0, 1, 0, -1};
101+
102+
public int[][] colorBorder(int[][] grid, int r0, int c0, int color) {
103+
boolean[][] vis = new boolean[grid.length][grid[0].length];
104+
dfs(grid, r0, c0, color, vis);
105+
return grid;
106+
}
107+
108+
private void dfs(int[][] grid, int i, int j, int color, boolean[][] vis) {
109+
vis[i][j] = true;
110+
int oldColor = grid[i][j];
111+
for (int k = 0; k < 4; ++k) {
112+
int x = i + dirs[k], y = j + dirs[k + 1];
113+
if (x >= 0 && x < grid.length && y >= 0 && y < grid[0].length) {
114+
if (!vis[x][y]) {
115+
if (grid[x][y] == oldColor) {
116+
dfs(grid, x, y, color, vis);
117+
} else {
118+
grid[i][j] = color;
119+
}
120+
}
121+
} else {
122+
grid[i][j] = color;
123+
}
124+
}
125+
}
126+
}
127+
```
128+
129+
### **C++**
130+
131+
```cpp
132+
class Solution {
133+
public:
134+
int m, n;
135+
vector<vector<int>> dirs = {{0, 1}, {0, - 1}, {1, 0}, {-1, 0}};
136+
137+
vector<vector<int>> colorBorder(vector<vector<int>>& grid, int row, int col, int color) {
138+
m = grid.size();
139+
n = grid[0].size();
140+
vector<vector<bool>> vis(m, vector<bool>(n, false));
141+
dfs(row, col, color, grid, vis);
142+
return grid;
143+
}
144+
145+
void dfs(int i, int j, int color, vector<vector<int>>& grid, vector<vector<bool>>& vis) {
146+
vis[i][j] = true;
147+
int oldColor = grid[i][j];
148+
for (auto& dir : dirs)
149+
{
150+
int x = i + dir[0], y = j + dir[1];
151+
if (x >= 0 && x < m && y >= 0 && y < n)
152+
{
153+
if (!vis[x][y])
154+
{
155+
if (grid[x][y] == oldColor) dfs(x, y, color, grid, vis);
156+
else grid[i][j] = color;
157+
}
158+
}
159+
else grid[i][j] = color;
160+
}
161+
}
162+
};
163+
```
105164
165+
### **Go**
166+
167+
```go
168+
func colorBorder(grid [][]int, row int, col int, color int) [][]int {
169+
m, n := len(grid), len(grid[0])
170+
vis := make([][]bool, m)
171+
for i := 0; i < m; i++ {
172+
vis[i] = make([]bool, n)
173+
}
174+
dirs := [4][2]int{{0, -1}, {0, 1}, {1, 0}, {-1, 0}}
175+
176+
var dfs func(i, j, color int)
177+
dfs = func(i, j, color int) {
178+
vis[i][j] = true
179+
oldColor := grid[i][j]
180+
for _, dir := range dirs {
181+
x, y := i+dir[0], j+dir[1]
182+
if x >= 0 && x < m && y >= 0 && y < n {
183+
if !vis[x][y] {
184+
if grid[x][y] == oldColor {
185+
dfs(x, y, color)
186+
} else {
187+
grid[i][j] = color
188+
}
189+
}
190+
} else {
191+
grid[i][j] = color
192+
}
193+
}
194+
}
195+
dfs(row, col, color)
196+
return grid
197+
}
106198
```
107199

108200
### **...**
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public:
3+
int m, n;
4+
vector<vector<int>> dirs = {{0, 1}, {0, - 1}, {1, 0}, {-1, 0}};
5+
6+
vector<vector<int>> colorBorder(vector<vector<int>>& grid, int row, int col, int color) {
7+
m = grid.size();
8+
n = grid[0].size();
9+
vector<vector<bool>> vis(m, vector<bool>(n, false));
10+
dfs(row, col, color, grid, vis);
11+
return grid;
12+
}
13+
14+
void dfs(int i, int j, int color, vector<vector<int>>& grid, vector<vector<bool>>& vis) {
15+
vis[i][j] = true;
16+
int oldColor = grid[i][j];
17+
for (auto& dir : dirs)
18+
{
19+
int x = i + dir[0], y = j + dir[1];
20+
if (x >= 0 && x < m && y >= 0 && y < n)
21+
{
22+
if (!vis[x][y])
23+
{
24+
if (grid[x][y] == oldColor) dfs(x, y, color, grid, vis);
25+
else grid[i][j] = color;
26+
}
27+
}
28+
else grid[i][j] = color;
29+
}
30+
}
31+
};

0 commit comments

Comments
(0)

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