8000 refactor 361 · Mars2018/Leetcode-3@bba1bde · GitHub
[go: up one dir, main page]

Skip to content

Commit bba1bde

Browse files
refactor 361
1 parent 4e4f547 commit bba1bde

File tree

1 file changed

+51
-48
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+51
-48
lines changed
Lines changed: 51 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.fishercoder.solutions;
22

33
/**
4+
* 361. Bomb Enemy
5+
*
46
* Given a 2D grid, each cell is either a wall 'W', an enemy 'E' or empty '0' (the number zero), return the maximum enemies you can kill using one bomb.
57
The bomb kills all the enemies in the same row and column from the planted point until it hits the wall since the wall is too strong to be destroyed.
68
Note that you can only put the bomb at an empty cell.
@@ -16,72 +18,73 @@
1618
*/
1719
public class _361 {
1820

19-
public int maxKilledEnemies(char[][] grid) {
20-
int m = grid.length;
21-
if (grid == null || m == 0) {
22-
return 0;
23-
}
24-
int n = grid[0].length;
21+
public static class Solution1 {
22+
public int maxKilledEnemies(char[][] grid) {
23+
int m = grid.length;
24+
if (grid == null || m == 0) {
25+
return 0;
26+
}
27+
int n = grid[0].length;
2528

26-
int[][] max = new int[m][n];
29+
int[][] max = new int[m][n];
2730

28-
for (int i = 0; i < m; i++) {
29-
for (int j = 0; j < n; j++) {
31+
for (int i = 0; i < m; i++) {
32+
for (int j = 0; j < n; j++) {
3033

31-
if (grid[i][j] == '0') {
32-
int count = 0;
34+
if (grid[i][j] == '0') {
35+
int count = 0;
3336

34-
//count all possible hits in its upward direction
35-
for (int k = j - 1; k >= 0; k--) {
36-
if (grid[i][k] == 'E') {
37-
count++;
38-
} else if (grid[i][k] == 'W') {
39-
break;
37+
//count all possible hits in its upward direction
38+
for (int k = j - 1; k >= 0; k--) {
39+
if (grid[i][k] == 'E') {
40+
count++;
41+
} else if (grid[i][k] == 'W') {
42+
break;
43+
}
4044
}
41-
}
4245

43-
//count all possible hits in its downward direction
44-
for (int k = j + 1; k < n; k++) {
45-
if (grid[i][k] == 'E') {
46-
count++;
47-
} else if (grid[i][k] == 'W') {
48-
break;
46+
//count all possible hits in its downward direction
47+
for (int k = j + 1; k < n; k++) {
48+
if (grid[i][k] == 'E') {
49+
count++;
50+
} else if (grid[i][k] == 'W') {
51+
break;
52+
}
4953
}
50-
}
5154

52-
//count all possible hits in its right direction
53-
for (int k = i + 1; k < m; k++) {
54-
if (grid[k][j] == 'E') {
55-
count++;
56-
} else if (grid[k][j] == 'W') {
57-
break;
55+
//count all possible hits in its right direction
56+
for (int k = i + 1; k < m; k++) {
57+
if (grid[k][j] == 'E') {
58+
count++;
59+
} else if (grid[k][j] == 'W') {
60+
break;
61+
}
5862
}
59-
}
6063

61-
//count all possible hits in its left direction
62-
for (int k = i - 1; k >= 0; k--) {
63-
if (grid[k][j] == 'E') {
64-
count++;
65-
} else if (grid[k][j] == 'W') {
66-
break;
64+
//count all possible hits in its left direction
65+
for (int k = i - 1; k >= 0; k--) {
66+
if (grid[k][j] == 'E') {
67+
count++;
68+
} else if (grid[k][j] == 'W') {
69+
break;
70+
}
6771
}
68-
}
69-
70-
max[i][j] = count;
7172

73+
max[i][j] = count;
74+
}
7275
}
7376
}
74-
}
7577

76-
int result = 0;
78+
int result = 0;
7779

78-
for (int i = 0; i < m; i++) {
79-
for (int j = 0; j < n; j++) {
80-
if (max[i][j] > result) {
81-
result = max[i][j];
80+
for (int i = 0; i < m; i++) {
81+
for (int j = 0; j < n; j++) {
82+
if (max[i][j] > result) {
83+
result = max[i][j];
84+
}
8285
}
8386
}
87+
return result;
8488
}
85-
return result;
8689
}
8790
}

0 commit comments

Comments
 (0)
0