easy


  • 0
    Y

    public class Solution {
    public int islandPerimeter(int[][] grid) {
    int k=0,p=0;
    int n=grid.length;
    int m=grid[0].length;

    	int[][] a = new int[n+2][m+2];
        for(int i=1;i<=n;i++)
        	for(int j=1;j<=m;j++)
        		a[i][j]=grid[i-1][j-1];
        
        for(int i=1;i<n+1;i++){
        	for(int j=1;j<m+1;j++){
        		if(a[i][j]==1){
        			k++;
        			p+=Sec(a,i,j);
        		}
        	}
        }
       return 4*k-p;
    }
    public static int  Sec(int a[][],int i,int j){
    	int num=0;
    	if(a[i-1][j]==1) num++;
    	if(a[i][j-1]==1) num++;
    	if(a[i][j+1]==1) num++;
    	if(a[i+1][j]==1) num++;
    	return num;
    }
    

    }


Log in to reply
 

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