8000 :sparkles: Added solution to 661 · ninefive/awesome-golang-leetcode@f099aa0 · GitHub
[go: up one dir, main page]

Skip to content

Commit f099aa0

Browse files
author
Sathish Babu
committed
✨ Added solution to 661
1 parent a276ef4 commit f099aa0

File tree

3 files changed

+61
-9
lines changed

3 files changed

+61
-9
lines changed

src/0661.Image-Smoother/README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@
88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
11+
Input:
12+
[[1,1,1],
13+
[1,0,1],
14+
[1,1,1]]
15+
Output:
16+
[[0, 0, 0],
17+
[0, 0, 0],
18+
[0, 0, 0]]
1319
```
1420

1521
## 题意

src/0661.Image-Smoother/Solution.go

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,52 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(M [][]int) [][]int {
4+
m, n := len(M), len(M[0])
5+
N := make([][]int, m)
6+
for i := range N {
7+
N[i] = make([]int, n)
8+
}
9+
for i := 0; i < m; i++ {
10+
for j := 0; j < n; j++ {
11+
N[i][j] = getSurrounding(M, i, j, m, n)
12+
}
13+
}
14+
return N
15+
}
16+
17+
func getSurrounding(M [][]int, i, j, m, n int) int {
18+
total, c := M[i][j], 1
19+
if i-1 >= 0 {
20+
total += M[i-1][j]
21+
c++
22+
if j-1 >= 0 {
23+
total += M[i-1][j-1]
24+
c++
25+
}
26+
if j+1 < n {
27+
total += M[i-1][j+1]
28+
c++
29+
}
30+
}
31+
if i+1 < m {
32+
total += M[i+1][j]
33+
c++
34+
if j-1 >= 0 {
35+
total += M[i+1][j-1]
36+
c++
37+
}
38+
if j+1 < n {
39+
total += M[i+1][j+1]
40+
c++
41+
}
42+
}
43+
if j-1 >= 0 {
44+
total += M[i][j-1]
45+
c++
46+
}
47+
if j+1 < n {
48+
total += M[i][j+1]
49+
c++
50+
}
51+
return total / c
552
}

src/0661.Image-Smoother/Solution_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs [][]int
14+
expect [][]int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase", [][]int{{2, 3, 4}, {5, 6, 7}, {8, 9, 10}, {11, 12, 13}, {14, 15, 16}}, [][]int{{4, 4, 5}, {5, 6, 6}, {8, 9, 9}, {11, 12, 12}, {13, 13, 14}}},
17+
{"TestCase", [][]int{{1, 1, 1}, {1, 0, 1}, {1, 1, 1}}, [][]int{{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}},
1918
}
2019

2120
// 开始测试

0 commit comments

Comments
 (0)
0