File tree Expand file tree Collapse file tree 1 file changed +52
-0
lines changed Expand file tree Collapse file tree 1 file changed +52
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ public int [][] imageSmoother (int [][] img ) {
3+ int ROWS = img .length , COLS = img [0 ].length ;
4+ int [][] res = new int [ROWS ][COLS ];
5+
6+ for (int r = 0 ; r < ROWS ; r ++){
7+ for (int c = 0 ; c < COLS ; c ++){
8+ int total = 0 , cnt = 0 ;
9+ for (int i = r - 1 ; i < r + 2 ; i ++){
10+ for (int j = c - 1 ; j < c + 2 ; j ++){
11+ if (i < 0 || i == ROWS || j < 0 || j == COLS )
12+ continue ;
13+ total += img [i ][j ];
14+ cnt += 1 ;
15+ }
16+ }
17+ res [r ][c ] = total / cnt ;
18+ }
19+ }
20+ return res ;
21+ }
22+ }
23+
24+ // Optimal Solution
25+
26+ class Solution {
27+ public int [][] imageSmoother (int [][] img ) {
28+ int ROWS = img .length , COLS = img [0 ].length ;
29+
30+ for (int r = 0 ; r < ROWS ; r ++){
31+ for (int c = 0 ; c < COLS ; c ++){
32+ int total = 0 , cnt = 0 ;
33+
34+ for (int i = r - 1 ; i < r + 2 ; i ++){
35+ for (int j = c - 1 ; j < c + 2 ; j ++){
36+ if (i < 0 || i == ROWS || j < 0 || j == COLS )
37+ continue ;
38+ total += img [i ][j ] % 256 ;
39+ cnt += 1 ;
40+ }
41+ }
42+ img [r ][c ] = img [r ][c ] ^ (total / cnt ) << 8 ;
43+ }
44+ }
45+ for (int r = 0 ; r < ROWS ; r ++){
46+ for (int c = 0 ; c < COLS ; c ++){
47+ img [r ][c ] = img [r ][c ] >> 8 ;
48+ }
49+ }
50+ return img ;
51+ }
52+ }
You can’t perform that action at this time.
0 commit comments