Leetcode 361 Boom enemies

Leetcode 361 Boom enemies_第1张图片

 public static int maxKilledEnemies(char[][] A){
        if (A==null || A.length ==0 ||A[0].length ==0 ){
            return 0;
        }
        int m = A.length;
        int n = A[0].length;

        int [][]up = new int[m][n];
        int [][]down = new int[m][n];
        int [][]left = new int[m][n];
        int [][]right = new int[m][n];

        //up
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (A[i][j]=='W'){
                    up[i][j] = 0;
                    continue;
                }
                up[i][j] = A[i][j]=='E'?1:0;
                if (i>0){
                    up[i][j] += up[i-1][j];
                }
            }
        }

        //down
        for (int i = m-1; i >=0; i--) {
            for (int j = 0; j < n; j++) {
                if (A[i][j]=='W'){
                    down[i][j] = 0;
                    continue;
                }
                down[i][j] = A[i][j]=='E'?1:0;//i==m-1的边界
                if (i0){
                    left[i][j] += left[i][j-1];
                }
            }
        }
        //right
        for (int i = 0; i < m; i++) {
            for (int j = n-1; j>=0; j--) {
                if (A[i][j]=='W'){
                    up[i][j] = 0;
                    continue;
                }
                right[i][j] = A[i][j]=='E'?1:0;
                if (j

你可能感兴趣的:(算法)