java 361. Bomb Enemy(1st).java

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 361. Bomb Enemy(1st).java相关的知识,希望对你有一定的参考价值。

public class Solution {
    public int maxKilledEnemies(char[][] grid) {
        if (grid == null || grid.length < 1 || grid[0].length < 1) return 0;
        int m = grid.length;
        int n = grid[0].length;
        int res = 0;
        int rowhit = 0;
        int[] colhits = new int[n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (j == 0 || grid[i][j - 1] == 'W') {
                    rowhit = 0;
                    for (int k = j; k < n && grid[i][k] != 'W'; k++) {
                        if (grid[i][k] == 'E') rowhit++;
                    }
                }
                if (i == 0 || grid[i - 1][j] == 'W') {
                    colhits[j] = 0;
                    for (int k = i; k < m && grid[k][j] != 'W'; k++) {
                        if (grid[k][j] == 'E') colhits[j]++;
                    }
                }
                if (grid[i][j] == '0') {
                    res = Math.max(res, rowhit + colhits[j]);
                } 
            }
        }
        return res;
    }
}
public class Solution {
    public int maxKilledEnemies(char[][] grid) {
        if (grid.length == 0 || grid[0].length == 0)    return 0;
        int m = grid.length, n = grid[0].length;
        int[] cols = new int[grid[0].length];
        int curRow = 0;
        
        for (int i = 0; i < m; i++) {
            curRow = 0;
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == 'E') {
                    cols[j] ++;
                    curRow ++;
                } else if (grid[i][j] == 'W') {
                    cols[j] = 0;
                    curRow = 0;
                } else {
                    grid[i][j] = (char)('X' + cols[j] + curRow);
                }
            }
        }
        
        int res = 0;
        Arrays.fill(cols, 0);
        for (int i = m - 1; i >= 0; i--) {
            curRow = 0;
            for (int j = n - 1; j >= 0; j--) {
                if (grid[i][j] == 'E') {
                    cols[j] ++;
                    curRow ++;
                } else if (grid[i][j] == 'W') {
                    cols[j] = 0;
                    curRow = 0;
                } else {
                    res = Math.max(res, grid[i][j] - 'X' + cols[j] + curRow);
                }
            }
        }
        
        return res;
    }
}

以上是关于java 361. Bomb Enemy(1st).java的主要内容,如果未能解决你的问题,请参考以下文章

java 361. Bomb Enemy(1st).java

java 361. Bomb Enemy(1st).java

java 361. Bomb Enemy(1st).java

java 361. Bomb Enemy(1st).java

361. Bomb Enemy

361. Bomb Enemy