simple java solution


  • 1
    2
    public class Solution {
        public int islandPerimeter(int[][] grid) {
            int ret = 0;
            int row = grid.length;
            int col = grid[0].length;
            for (int i = 0; i < row; i++) {
                for (int j = 0; j < col; j++) {
                    if (grid[i][j] == 1) {
                        ret += 4;
                        if (i != 0 && grid[i - 1][j] == 1) {
                            ret -= 2;
                        }
                        if (j != 0 && grid[i][j - 1] == 1) {
                            ret -= 2;
                        }
                    }
                }
            }
            return ret;
        }
    }
    

Log in to reply
 

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