Java so-easy solution


  • 0
    Z
            int rows = grid.length;
            if(rows == 0){
                return 0;
            }
            int cols = grid[0].length;
            int total = 0;
            int count = 0;
            for(int i = 0; i < rows; i ++){
                for(int j = 0; j < cols; j ++){
                    int n = grid[i][j];
                    if(n != 1){
                        continue ;
                    }
                    total ++;
                    if(i > 0 && grid[i - 1][j] == 1){
                        count ++;
                    }
                    if(j > 0 && grid[i][j - 1] == 1){
                        count ++;
                    }
                    if(i + 1 < rows && grid[i + 1][j] == 1){
                        count ++;
                    }
                    if(j + 1 < cols && grid[i][j + 1] == 1){
                        count ++;
                    }
                }
            }
            return total * 4 - count;
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.