Brute force java solution


  • 0
    F
    public class Solution {
        int sum = 0;
        public int islandPerimeter(int[][] grid) {
            for(int i = 0;i<grid.length;i++)
                for(int j = 0;j<grid[i].length;j++){
                    if(grid[i][j] == 1) helper(grid,i,j);
                }
                return sum;
        }
        public void helper(int[][] grid,int i,int j){
           if(i == 0) sum++;
           if(i == grid.length-1) sum++;
           if(j == 0) sum++;
           if(j == grid[0].length-1) sum++;
           if(i-1 >=0 && grid[i-1][j] == 0) sum++;
           if(i +1 < grid.length && grid[i+1][j] == 0) sum++;
           if(j-1>=0 && grid[i][j-1] == 0) sum++;
           if(j+1 < grid[0].length && grid[i][j+1] ==0) sum++;
        }
    }
    

Log in to reply
 

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