Easy and Fast Java Solution


  • 0
    P
    public int islandPerimeter(int[][] grid) {
            if(grid==null)	return 0;
    		int res=0;
            int ro=grid.length;
            int co=grid[0].length;
            int[][]	temp=new int[ro+2][co+2];
            for(int	i=1;i<ro+1;i++){
            	for(int j=1;j<co+1;j++){
            		temp[i][j]=grid[i-1][j-1];
            	}
            }
            for(int m=1;m<ro+1;m++){
            	for(int n=1;n<co+1;n++){
            		if(temp[m][n]!=0){
            		res+=(temp[m][n]-temp[m][n-1]+temp[m][n]-temp[m][n+1]
            			 +temp[m][n]-temp[m-1][n]+temp[m][n]-temp[m+1][n]);
            		}
            		}
            }
            return res;
        }
    ····

Log in to reply
 

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