payal-kothari
7/15/2017 - 5:09 AM

grid.length --- to get the number of rows grid[0].length ---- to get the number of cols From https://leetcode.com/problems/island-per

grid.length --- to get the number of rows grid[0].length ---- to get the number of cols

From https://leetcode.com/problems/island-perimeter/#/description

public class Solution {
    public int islandPerimeter(int[][] grid) {
        
        int per = 0;
        
        for (int i =0; i< grid.length ; i++){       // grid.length     to get the number of rows
            for(int j =0; j< grid[0].length; j++){  // grid[0].length    to get the number of cols
                
                if (grid[i][j] == 1){
                    
                    per = per + 4;
                    
                    if( i != grid.length -1 && grid[i+1][j] == 1){
                        per--;
                    }
                    if ( j != 0 && grid[i][j-1] == 1){
                        per--;
                    }
                    if ( j != grid[0].length - 1 && grid[i][j+1] ==1 ){
                        per--;
                    }
                    if( i != 0 && grid[i-1][j] ==1 ){
                        per--;
                    }
                    
                }
            }
        }
        
        
        return per;
    }
}