Skip to content

Commit ce19ec2

Browse files
committed
feat: add the solution of Image Smoother(661) with Java.
1 parent 40777a3 commit ce19ec2

File tree

3 files changed

+88
-25
lines changed

3 files changed

+88
-25
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
| [645][645-question] | [Set Mismatch][645-tips] | [][645-java] | [][645-js] | |
6767
| [653][653-question] | [Two Sum IV - Input is a BST][653-tips] | [][653-java] | [][653-js] | |
6868
| [657][657-question] | [Judge Route Circle][657-tips] | [][657-java] | [][657-js] | |
69-
| [661][661-question] | [Image Smoother][661-tips] | | [][661-js] | |
69+
| [661][661-question] | [Image Smoother][661-tips] | [][661-java] | [][661-js] | |
7070
| [665][665-question] | [Non-decreasing Array][665-tips] | | [][665-js] | |
7171
| [669][669-question] | [Trim a Binary Search Tree][669-tips] | | [][669-js] | |
7272
| [671][671-question] | [Second Minimum Node In a Binary Tree][671-tips] | | [][671-js] | |
@@ -457,6 +457,7 @@
457457
[645-java]: ./src/_645/Solution.java
458458
[653-java]: ./src/_653/Solution.java
459459
[657-java]: ./src/_657/Solution.java
460+
[661-java]: ./src/_661/Solution.java
460461
[728-java]: ./src/_728/Solution.java
461462
[771-java]: ./src/_771/Solution.java
462463
[804-java]: ./src/_804/Solution.java

src/_661/Solution.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package _661;
2+
3+
class Solution {
4+
public int[][] imageSmoother(int[][] M) {
5+
int[][] result = new int[M.length][M[0].length];
6+
7+
int sum;
8+
int count;
9+
int xLen = M.length;
10+
int yLen = M[0].length;
11+
int nx, ny;
12+
13+
for (int x = 0; x < M.length; x++) {
14+
for (int y = 0; y < M[x].length; y++) {
15+
sum = 0;
16+
count = 0;
17+
for (int i = -1; i <= 1; i++) {
18+
for (int j = -1; j <= 1; j++) {
19+
nx = x+i;
20+
ny = y+j;
21+
if (nx >= 0 && nx < xLen && ny >= 0 && ny < yLen) {
22+
sum += M[nx][ny];
23+
count++;
24+
}
25+
}
26+
}
27+
28+
result[x][y] = sum / count;
29+
}
30+
}
31+
32+
return result;
33+
}
34+
}

tips/661/README.md

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,74 @@
1-
[xxxx][title]
1+
[Image Smoother][title]
22

33
## Description
4-
// 抄题目
5-
4+
Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother to make the gray scale of each cell becomes the average gray scale (rounding down) of all the 8 surrounding cells and itself. If a cell has less than 8 surrounding cells, then use as many as you can.
65

76
**Example:**
87

98
```
10-
// 抄Example
9+
Input:
10+
[[1,1,1],
11+
[1,0,1],
12+
[1,1,1]]
13+
Output:
14+
[[0, 0, 0],
15+
[0, 0, 0],
16+
[0, 0, 0]]
17+
Explanation:
18+
For the point (0,0), (0,2), (2,0), (2,2): floor(3/4) = floor(0.75) = 0
19+
For the point (0,1), (1,0), (1,2), (2,1): floor(5/6) = floor(0.83333333) = 0
20+
For the point (1,1): floor(8/9) = floor(0.88888889) = 0
1121
```
1222

1323
**Note:**
14-
// Note
1524

16-
**Tags:** // tags
25+
1. The value in the given matrix is in the range of [0, 255].
26+
2. The length and width of the given matrix are in the range of [1, 150].
1727

28+
**Tags:** [Array](https://leetcode.com/tag/array/)
1829

19-
## 思路 1
20-
// 贴一些关键代码,说一些解题思路
21-
// (同一种语言可以写多种思路,与某种语言思路相同的另一种语言的思路无须赘述,但可以把代码贴在后面)
22-
```java
30+
## 思路
2331

24-
```
25-
```javascript
32+
题意为对像素进行平滑化处理。平滑化后的像素值为周围的8个像素加上自己的平均值,再取整。
2633

27-
```
34+
**Java**
2835

29-
## 思路 2
30-
// 贴一些关键代码,说一些解题思路
3136
```java
32-
33-
```
34-
35-
## 思路 3
36-
// 贴一些关键代码,说一些解题思路
37-
```kotlin
38-
37+
class Solution {
38+
public int[][] imageSmoother(int[][] M) {
39+
int[][] result = new int[M.length][M[0].length];
40+
41+
int sum;
42+
int count;
43+
int xLen = M.length;
44+
int yLen = M[0].length;
45+
int nx, ny;
46+
for (int x = 0; x < M.length; x++) {
47+
for (int y = 0; y < M[x].length; y++) {
48+
sum = 0;
49+
count = 0;
50+
for (int i = -1; i <= 1; i++) {
51+
for (int j = -1; j <= 1; j++) {
52+
nx = x+i;
53+
ny = y+j;
54+
if (nx >= 0 && nx < xLen && ny >= 0 && ny < yLen) {
55+
sum += M[nx][ny];
56+
count++;
57+
}
58+
}
59+
}
60+
result[x][y] = sum / count;
61+
}
62+
}
63+
64+
return result;
65+
}
66+
}
3967
```
4068

4169
## 结语
42-
70+
4371
如果你同我们一样热爱数据结构、算法、LeetCode,可以关注我们 GitHub 上的 LeetCode 题解:[LeetCode-Solution][ls]
4472

45-
[title]: https://leetcode.com/problems/xxxx
73+
[title]: https://leetcode.com/problems/image-smoother/description/
4674
[ls]: https://github.com/SDE603/LeetCode-Solution

0 commit comments

Comments
 (0)