Java Solution for a beginner


  • 0
    R

    public class Solution {

    public static int valueIn(int[][] g,int i,int j){
        
         if(!isValid(g,i,j)){
          return 1;
         }
        else if(g[i][j] == 0){
            return 1;
        }
        else if(g[i][j]==1){
            return 0;
        }
        else 
        return 1;
        
        
        
    }
    
    public static boolean isValid(int[][]g,int i,int j){
        
        
        return i >= 0 && i < g.length && j >=0 && j < g[0].length; 
        
        
    }
    
    
    public int islandPerimeter(int[][] grid) {
        
        int x = 0;
        for(int i = 0; i < grid.length; i++){
            
            for(int j = 0; j < grid[0].length; j++){
                
                
                if(grid[i][j] == 1){
                x += valueIn(grid, i-1, j) + valueIn(grid, i,j-1) + valueIn(grid, i+1,j) + valueIn(grid, i,j+1);
                }
                
            }
            
            
            
        }
        
        
        return x;
        
    }
    

    }


Log in to reply
 

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