LeetCode: 463. Island Perimeter
You are given a map in form of a two-dimensional integer grid where 1
represents land and 0 represents water. Grid cells are connected
horizontally/vertically (not diagonally). The grid is completely
surrounded by water, and there is exactly one island (i.e., one or
more connected land cells). The island doesn’t have “lakes” (water
inside that isn’t connected to the water around the island). One cell
is a square with side length 1. The grid is rectangular, width and
height don’t exceed 100. Determine the perimeter of the island.Example:
[[0,1,0,0], [1,1,1,0], [0,1,0,0], [1,1,0,0]]
Answer: 16 Explanation: The perimeter is the 16 yellow stripes in the
image below:
自己的答案:
public class Solution {
public int islandPerimeter(int[][] grid) {
int length = grid.length;
if (length <= 0) {
return 0;
}
int width = grid[0].length;
if (width <= 0) {
return 0;
}
if (length == 1 && width == 1) {
return 4;
}
if (length == 1) {
int result = 0;
if (grid[0][0] == 1) {
if (grid[0][1] == 1) {
result += 3;
} else {
result += 4;
}
}
if (grid[0][width-1] == 1) {
if (grid[0][width-2] == 1) {
result += 3;
} else {
result += 4;
}
}
for (int i = 1; i < width-1; i++) {
if (grid[0][i] == 1) {
result += 2;
if (grid[0][i-1] == 0) {
result += 1;
}
if (grid[0][i+1] == 0) {
result += 1;
}
}
}
return result;
}
if (width == 1) {
int result = 0;
if (grid[0][0] == 1) {
if (grid[1][0] == 1) {
result += 3;
} else {
result += 4;
}
}
if (grid[length-1][0] == 1) {
if (grid[length-2][0] == 1) {
result += 3;
} else {
result += 4;
}
}
for (int i = 1; i < length -1; i++) {
if (grid[i][0] == 1) {
result += 2;
if (grid[i-1][0] == 0) {
result += 1;
}
if (grid[i+1][0] == 0) {
result += 1;
}
}
}
return result;
}
int result = 0;
if (grid[0][0] == 1) {
result += 2;
if (grid[0][1] == 0) {
result += 1;
}
result += (grid[1][0] == 0 ? 1 : 0);
}
if (grid[0][width-1] == 1) {
result += 2;
result += (grid[0][width-2] == 0 ? 1 : 0);
result += (grid[1][width-1] == 0 ? 1 : 0);
}
if (grid[length-1][0] == 1) {
result += 2;
result += (grid[length-1][1] == 0 ? 1 : 0);
result += (grid[length-2][0] == 0 ? 1 : 0);
}
if (grid[length-1][width-1] == 1) {
result += 2;
result += (grid[length-1][width-2] == 0 ? 1 : 0);
result += (grid[length-2][width-1] == 0 ? 1 : 0);
}
for (int i = 0; i < length; i += length-1) {
int temp = i == 0 ? 1 : -1;
for (int j = 1; j < width-1; j++) {
if (grid[i][j] == 1) {
result += 1;
result += (grid[i][j-1] == 0 ? 1 : 0);
result += (grid[i][j+1] == 0 ? 1 : 0);
result += (grid[i+temp][j] == 0 ? 1 : 0);
}
}
}
for (int j = 0; j < width; j += width-1) {
int temp = j == 0 ? 1 : -1;
for (int i = 1; i < length-1; i++) {
if (grid[i][j] == 1) {
result += 1;
result += (grid[i-1][j] == 0 ? 1 : 0);
result += (grid[i+1][j] == 0 ? 1 : 0);
result += (grid[i][j+temp] == 0 ? 1 : 0);
}
}
}
for (int i = 1; i < length-1; i++) {
for (int j = 1; j < width-1; j++) {
if (grid[i][j] == 1) {
result += (grid[i-1][j] == 0 ? 1 : 0);
result += (grid[i+1][j] == 0 ? 1 : 0);
result += (grid[i][j-1] == 0 ? 1 : 0);
result += (grid[i][j+1] == 0 ? 1 : 0);
}
}
}
return result;
}
}
速度最快的答案:
public class Solution {
public int islandPerimeter(int[][] grid) {
int result = 0;
for(int i = 0; i < grid.length; i++){
for(int j = 0; j < grid[i].length; j++){
if(grid[i][j] == 1){
result += 4;
if(i < grid.length-1 && grid[i+1][j] == 1) result -= 2;
if(j < grid[i].length-1 && grid[i][j+1] == 1) result -= 2;
}
}
}
return result;
}
}