Simple and clean code, beats 98.7%


  • 0
    P

    public class Solution {
    public int islandPerimeter(int[][] x) {
    int rows = x.length-1;
    int cols = (x[0].length)-1;
    int i =0; int count =0;

        while(i<=rows)
        {int j=0;
            while(j<=cols)
            {
                if(x[i][j] == 1) {count=count+4;
                
                if(i-1>=0)
                   {if(x[i-1][j] == 1) count--;}
                   
                 if(j-1 >=0)
                  {if(x[i][j-1] == 1) count--;}
                  
                  if(i+1<= rows)
                  {if(x[i+1][j] ==1) count--;}
                  
                  if(j+1<= cols)
                  {if(x[i][j+1] == 1) count--;}
                  
                }
            j++;}
        i++;}
        
        return count;
        
    }
    

    }


Log in to reply
 

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