Skip to content

Commit a902da5

Browse files
committed
f
1 parent 446b2c8 commit a902da5

File tree

3 files changed

+74
-75
lines changed

3 files changed

+74
-75
lines changed

solution/src/main/java/com/inuker/solution/BombEnemy.java

Lines changed: 29 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,55 +7,39 @@
77
public class BombEnemy {
88

99
public int maxKilledEnemies(char[][] grid) {
10-
if (grid.length == 0) {
11-
return 0;
12-
}
13-
14-
int row = grid.length, col = grid[0].length;
15-
16-
int result = 0, rowhits = 0;
17-
int[] colhits = new int[col];
18-
19-
for (int i = 0; i < row; i++) {
20-
// 这个循环里每次都从墙或边界开始,是为了避免重复计算
21-
for (int j = 0; j < col; j++) {
22-
/** 如果当前是墙则直接跳过 */
23-
if (grid[i][j] == 'W') {
24-
continue;
25-
}
26-
27-
// rowhits表示在当前点横向能杀伤的人数
28-
// 如果左边靠着墙或边界则统计往右延伸能杀多少敌人
29-
// 如果不是靠着墙,则不用统计了,直接复用本行之前的结果
30-
if (j == 0 || grid[i][j - 1] == 'W') {
31-
/** 注意这里rowhits要清零 */
32-
rowhits = 0;
33-
// 往右延伸一直到墙或边界
34-
for (int k = j; k < col && grid[i][k] != 'W'; k++) {
35-
rowhits += (grid[i][k] == 'E') ? 1 : 0;
36-
}
37-
}
38-
39-
// colhits表示本行第j列纵向能杀伤的人数
40-
// 如果上面靠着墙或边界则统计往下延伸能杀伤多少人
41-
// 如果不是靠着墙则不用统计了,直接复用本列之前的结果
42-
if (i == 0 || grid[i - 1][j] == 'W') {
43-
/** 注意这里colhits[j]要清零 */
44-
colhits[j] = 0;
45-
// 往下延伸到边界或者墙
46-
for (int k = i; k < row && grid[k][j] != 'W'; k++) {
47-
colhits[j] += (grid[k][j] == 'E') ? 1 : 0;
48-
}
49-
}
50-
51-
// 如果当前可以放炸弹,横向和纵向总杀伤人数和
52-
/** 这里要判断是否可以放炸弹 */
10+
int max = 0;
11+
for (int i = 0; i < grid.length; i++) {
12+
for (int j = 0; j < grid[0].length; j++) {
5313
if (grid[i][j] == '0') {
54-
result = Math.max(result, rowhits + colhits[j]);
14+
max = Math.max(max, killedEnemies(grid, i, j));
5515
}
5616
}
5717
}
18+
return max;
19+
}
5820

59-
return result;
21+
private int killedEnemies(char[][] grid, int i, int j) {
22+
int count = 0;
23+
for (int k = j - 1; k >= 0 && grid[i][k] != 'W'; k--) {
24+
if (grid[i][k] == 'E') {
25+
count++;
26+
}
27+
}
28+
for (int k = j + 1; k < grid[0].length && grid[i][k] != 'W'; k++) {
29+
if (grid[i][k] == 'E') {
30+
count++;
31+
}
32+
}
33+
for (int k = i - 1; k >= 0 && grid[k][j] != 'W'; k--) {
34+
if (grid[k][j] == 'E') {
35+
count++;
36+
}
37+
}
38+
for (int k = i + 1; k < grid.length && grid[k][j] != 'W'; k++) {
39+
if (grid[k][j] == 'E') {
40+
count++;
41+
}
42+
}
43+
return count;
6044
}
6145
}

solution/src/main/java/com/inuker/solution/QueueReconstructionByHeight.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77

88
/**
99
* Created by liwentian on 2017/9/1.
10+
* <p>
11+
* 这题核心思路是:
12+
* 1,先取出高度最高的那一组,如果有若干个高度相同的,则按k升序排列,这就是他们之后的相对顺序了。
13+
* 2,排除刚取出的那些组,从剩余的组中再取出最高的,仍按k升序排列,然后依次插入
14+
* <p>
15+
* 这题核心思路是:
16+
* 1,先取出高度最高的那一组,如果有若干个高度相同的,则按k升序排列,这就是他们之后的相对顺序了。
17+
* 2,排除刚取出的那些组,从剩余的组中再取出最高的,仍按k升序排列,然后依次插入
1018
*/
1119

1220
/**
@@ -21,16 +29,19 @@
2129
*/
2230
public class QueueReconstructionByHeight {
2331

32+
// [[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]
33+
// [7,0], [7,1], [6,1], [5,0], [5,2], [4,4]
34+
// [5,0], [7,0], [5,2], [6,1], [4,4], [7,1]
2435
public int[][] reconstructQueue(int[][] people) {
25-
Arrays.sort(people,new Comparator<int[]>(){
36+
Arrays.sort(people, new Comparator<int[]>() {
2637
@Override
27-
public int compare(int[] o1, int[] o2){
28-
return o1[0]!=o2[0]?-o1[0]+o2[0]:o1[1]-o2[1];
38+
public int compare(int[] o1, int[] o2) {
39+
return o1[0] != o2[0] ? o2[0] - o1[0] : o1[1] - o2[1];
2940
}
3041
});
3142
List<int[]> res = new LinkedList<>();
32-
for(int[] cur : people){
33-
res.add(cur[1],cur);
43+
for (int[] cur : people) {
44+
res.add(cur[1], cur);
3445
}
3546
return res.toArray(new int[people.length][]);
3647
}

test/src/main/java/com/inuker/test/main.java

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,38 +22,42 @@
2222
public class main {
2323

2424
public static void main(String[] args) {
25-
Codec c = new Codec();
26-
String s = c.encode(Arrays.asList("hello", "how", "are", "you"));
27-
System.out.println(s);
28-
for (String ss : c.decode(s)) {
29-
System.out.println(ss);
30-
}
3125
}
3226

33-
class ValidWordAbbr {
34-
35-
HashMap<String, Set<String>> map = new HashMap<>();
36-
37-
public ValidWordAbbr(String[] dictionary) {
38-
for (String s : dictionary) {
39-
String abbr = getAbbr(s);
40-
Set<String> set = map.getOrDefault(abbr, new HashSet<String>());
41-
set.add(s);
42-
map.put(abbr, set);
27+
public int maxKilledEnemies(char[][] grid) {
28+
int max = 0;
29+
for (int i = 0; i < grid.length; i++) {
30+
for (int j = 0; j < grid[0].length; j++) {
31+
if (grid[i][j] == '0') {
32+
max = Math.max(max, killedEnemies(grid, i, j));
33+
}
4334
}
4435
}
36+
return max;
37+
}
4538

46-
private String getAbbr(String s) {
47-
return s.length() > 2 ? s.substring(0, 1) + (s.length() - 2) + s.substring(s.length() - 1) : s;
39+
private int killedEnemies(char[][] grid, int i, int j) {
40+
int count = 0;
41+
for (int k = j - 1; k >= 0 && grid[i][k] != 'W'; k--) {
42+
if (grid[i][k] == 'E') {
43+
count++;
44+
}
4845
}
49-
50-
public boolean isUnique(String word) {
51-
String abbr = getAbbr(word);
52-
if (!map.containsKey(abbr)) {
53-
return true;
46+
for (int k = j + 1; k < grid[0].length && grid[i][k] != 'W'; k++) {
47+
if (grid[i][k] == 'E') {
48+
count++;
49+
}
50+
}
51+
for (int k = i - 1; k >= 0 && grid[k][j] != 'W'; k--) {
52+
if (grid[k][j] == 'E') {
53+
count++;
54+
}
55+
}
56+
for (int k = i + 1; k < grid.length && grid[k][j] != 'W'; k++) {
57+
if (grid[k][j] == 'E') {
58+
count++;
5459
}
55-
Set<String> set = map.get(abbr);
56-
return set.size() == 1 && set.contains(word);
5760
}
61+
return count;
5862
}
5963
}

0 commit comments

Comments
 (0)