Java Simple Solution


  • 0
    D
    public class Solution {
        public int islandPerimeter(int[][] grid) {
        	if(grid == null || grid.length == 0|| grid[0].length == 0)
        		return 0;
        	int perimeter = 0;
        	int n = grid.length;
        	int m = grid[0].length;
            for(int i = 0; i < n; i++) {
            	for(int j = 0; j < m; j++) {
            		if(grid[i][j] == 1) {
            			if(i == 0 || grid[i - 1][j] == 0)
            				perimeter++;
            			if(i == n -1 || grid[i + 1][j] == 0)
            				perimeter++;
            			if(j == 0 || grid[i][j - 1] == 0)
            				perimeter++;
            			if(j == m - 1|| grid[i][j + 1] == 0)
            				perimeter ++;
            		}
            	}
            }
            return perimeter;
        }
    }
    

Log in to reply
 

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