Skip to content

Commit ee43f44

Browse files
committed
ImageSmoother661
1 parent f13e03c commit ee43f44

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@
4444
| [Graph](https://github.com/fluency03/leetcode-java/blob/master/src/graph) |
4545

4646

47-
# Total: 294
47+
# Total: 295
4848

4949
| Easy | Medium | Hard | - |
5050
|:----:|:-------:|:----:|:-:|
51-
| 77 | 162 | 52 | 3 |
51+
| 78 | 162 | 52 | 3 |
5252

5353

5454
| Question | Solution | Difficulty |
@@ -324,6 +324,7 @@
324324
| [657. Judge Route Circle](https://leetcode.com/problems/judge-route-circle/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/JudgeRouteCircle657.java) | Easy |
325325
| [658. Find K Closest Elements](https://leetcode.com/problems/find-k-closest-elements//) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/FindKClosestElements658.java) | Medium |
326326
| [659. Split Array into Consecutive Subsequences](https://leetcode.com/problems/split-array-into-consecutive-subsequences/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/SplitArrayIntoConsecutiveSubsequences659.java) | Medium |
327+
| [661. Image Smoother](https://leetcode.com/problems/image-smoother/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/ImageSmoother661.java) | Easy |
327328
| [671. Second Minimum Node In a Binary Tree](https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/SecondMinimumNodeInABinaryTree671.java) | Easy |
328329
| [674. Longest Continuous Increasing Subsequence](https://leetcode.com/problems/longest-continuous-increasing-subsequence/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/LongestContinuousIncreasingSubsequence674.java) | Easy |
329330
| [680. Valid Palindrome II](https://leetcode.com/problems/valid-palindrome-ii/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/ValidPalindromeII680.java) | Easy |

src/ImageSmoother661.java

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* Given a 2D integer matrix M representing the gray scale of an image, you
3+
* need to design a smoother to make the gray scale of each cell becomes the
4+
* average gray scale (rounding down) of all the 8 surrounding cells and itself.
5+
* If a cell has less than 8 surrounding cells, then use as many as you can.
6+
*
7+
* Example 1:
8+
*
9+
* Input:
10+
* [[1,1,1],
11+
* [1,0,1],
12+
* [1,1,1]]
13+
*
14+
* Output:
15+
* [[0, 0, 0],
16+
* [0, 0, 0],
17+
* [0, 0, 0]]
18+
*
19+
* Explanation:
20+
* For the point (0,0), (0,2), (2,0), (2,2): floor(3/4) = floor(0.75) = 0
21+
* For the point (0,1), (1,0), (1,2), (2,1): floor(5/6) = floor(0.83333333) = 0
22+
* For the point (1,1): floor(8/9) = floor(0.88888889) = 0
23+
*
24+
* Note:
25+
* The value in the given matrix is in the range of [0, 255].
26+
* The length and width of the given matrix are in the range of [1, 150].
27+
*/
28+
29+
public class ImageSmoother661 {
30+
private int[][] dirs = new int[][]{{0,0}, {0,1}, {1,1}, {1,0}, {1,-1}, {0,-1}, {-1,0}, {-1,-1}, {-1,1}};
31+
public int[][] imageSmoother(int[][] M) {
32+
int lenI = M.length;
33+
int lenJ = M[0].length;
34+
int[][] newScale = new int[lenI][lenJ];
35+
36+
for (int i=0; i<lenI; i++) {
37+
for (int j=0; j<lenJ; j++) {
38+
int sum = 0;
39+
int count = 0;
40+
for (int[] dir: dirs) {
41+
int ii = i+dir[0];
42+
int jj = j+dir[1];
43+
if (ii >= 0 && jj >= 0 && ii < lenI && jj < lenJ) {
44+
sum += M[ii][jj];
45+
count++;
46+
}
47+
}
48+
newScale[i][j] = sum / count;
49+
}
50+
}
51+
return newScale;
52+
}
53+
}

0 commit comments

Comments
 (0)