Solution
public class Solution {
public int islandPerimeter(int[][] grid) {
int[] type = new int[5];
int length = grid.length;
int width = grid[0].length;
for (int i = 0; i < length; i++) {
for (int j = 0; j < width; j++) {
int edgeNum = 0;
if (grid[i][j] != 0) {
if (i == 0 || grid[i - 1][j] == 0) {
edgeNum++;
}
if (i == length - 1 || grid[i + 1][j] == 0) {
edgeNum++;
}
if (j == 0 || grid[i][j - 1] == 0) {
edgeNum++;
}
if (j == width - 1 || grid[i][j+1] == 0) {
edgeNum++;
}
type[edgeNum]++;
}
}
}
int perimeter = 0;
for (int i = 1; i < 5; i++) {
perimeter += i * type[i];
}
return perimeter;
}
}
Problem#1