Skip to content

Commit f1e25ec

Browse files
solves image smoother
1 parent cb2daef commit f1e25ec

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@
170170
| 645 | [Set Mismatch](https://leetcode.com/problems/set-mismatch) | [![Java](assets/java.png)](src/SetMismatch.java) [![Python](assets/python.png)](python/set_mismatch.py) |
171171
| 653 | [Two Sum IV - Input is a BST](https://leetcode.com/problems/two-sum-iv-input-is-a-bst) | [![Java](assets/java.png)](src/TwoSumIVInputIsABST.java) [![Python](assets/python.png)](python/two_sum_iv.py) |
172172
| 657 | [Robot Return to Origin](https://leetcode.com/problems/robot-return-to-origin) | [![Java](assets/java.png)](src/RobotReturnToOrigin.java) [![Python](assets/python.png)](python/robot_return_to_origin.py) |
173-
| 661 | [Image Smoother](https://leetcode.com/problems/image-smoother) | |
173+
| 661 | [Image Smoother](https://leetcode.com/problems/image-smoother) | [![Java](assets/java.png)](src/ImageSmoother.java) [![Python](assets/python.png)](python/image_smoother.py) |
174174
| 665 | [Non Deceasing Array](https://leetcode.com/problems/non-decreasing-array) | |
175175
| 669 | [Trim a Binary Search Tree](https://leetcode.com/problems/trim-a-binary-search-tree) | |
176176
| 671 | [Second Minimum Node in Binary Tree](https://leetcode.com/problems/second-minimum-node-in-a-binary-tree) | |

Diff for: python/image_smoother.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def imageSmoother(self, img: List[List[int]]) -> List[List[int]]:
6+
rows, columns = len(img), len(img[0])
7+
result = [[0 for i in range(columns)] for j in range(rows)]
8+
for row in range(rows):
9+
for column in range(columns):
10+
k_sum, count = 0, 0
11+
for k_row in range(row if row == 0 else row - 1, (row if row == rows - 1 else row + 1) + 1):
12+
for k_column in range(column if column == 0 else column - 1, (column if column == columns - 1 else column + 1) + 1):
13+
k_sum += img[k_row][k_column]
14+
count += 1
15+
result[row][column] = k_sum // count
16+
return result

Diff for: src/ImageSmoother.java

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public class ImageSmoother {
2+
public int[][] imageSmoother(int[][] img) {
3+
int[][] result = new int[img.length][img[0].length];
4+
int sum, count, rows = img.length, columns = img[0].length;
5+
for (int row = 0 ; row < rows; row++) {
6+
for (int column = 0 ; column < columns ; column++) {
7+
sum = count = 0;
8+
for (int k_row = row == 0 ? row : row - 1 ; k_row <= (row == rows - 1 ? row : row + 1) ; k_row++) {
9+
for (int k_column = column == 0 ? column : column - 1 ; k_column <= (column == columns - 1 ? column : column + 1) ; k_column++, count++) {
10+
sum += img[k_row][k_column];
11+
}
12+
}
13+
result[row][column] = sum / count;
14+
}
15+
}
16+
return result;
17+
}
18+
}

0 commit comments

Comments
 (0)