My easy java solution: just use a larger board and calculate directly

  • 0
    public class Solution {
        public int islandPerimeter(int[][] grid) {
            //rewrite the grid in a larger board
            int[][] board = new int[grid.length+2][grid[0].length+2];
            int result = 0;
            for(int i=0;i<grid.length;i++){
                for(int j=0;j<grid[0].length;j++){
            for(int i=1;i<=grid.length;i++){
                for(int j=1;j<=grid[0].length;j++){
                        if(board[i-1][j]==0) result++;
                        if(board[i+1][j]==0) result++;
                        if(board[i][j-1]==0) result++;
                        if(board[i][j+1]==0) result++;
            return result;

Log in to reply

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