leetcode 463. Island Perimeter
2017-02-28 16:22
381 查看
题目
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:
代码
public class Solution {
public int islandPerimeter(int[][] grid) {
int res = 0;
for (int i=0; i<grid.length; i++){
for(int j=0 ;j<grid[i].length; j++){
if (j == 0){
if(grid[i][j] == 1){
res += 1;
}
}
if(j == grid[i].length-1){
if(j>0){
if(grid[i][j] != grid[i][j-1]){
res += 1;
}
}
if(grid[i][j] == 1){
res += 1;
}
}
if(j>0){
if(j != 0&&j != grid[i].length-1){
if(grid[i][j] != grid[i][j-1]){
res += 1;
}
}
}
}
}
for(int i=0; i<grid[0].length; i++){
for(int j=0; j<grid.length; j++){
if(j == 0){
if(grid[j][i] == 1){
res += 1;
}
}
if(j == grid.length-1){
if(j>0){
if(grid[j][i] != grid[j-1][i]){
res += 1;
}
}
if(grid[j][i] == 1){
res += 1;
}
}
if(j>0){
if((j != 0)&&(j != grid.length-1)){
if(grid[j][i] != grid[j-1][i]){
res += 1;
}
}
}
}
}
return res;
}
public static void main(String[] args) {
// int[][] a= {{0,1,0,0},{1,1,1,0},{0,1,0,0},{1,1,0,0}};
int[][] a= {{1}};
System.out.println(new Solution().islandPerimeter(a));
}
}
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 res = 0;
for (int i=0; i<grid.length; i++){
for(int j=0 ;j<grid[i].length; j++){
if (j == 0){
if(grid[i][j] == 1){
res += 1;
}
}
if(j == grid[i].length-1){
if(j>0){
if(grid[i][j] != grid[i][j-1]){
res += 1;
}
}
if(grid[i][j] == 1){
res += 1;
}
}
if(j>0){
if(j != 0&&j != grid[i].length-1){
if(grid[i][j] != grid[i][j-1]){
res += 1;
}
}
}
}
}
for(int i=0; i<grid[0].length; i++){
for(int j=0; j<grid.length; j++){
if(j == 0){
if(grid[j][i] == 1){
res += 1;
}
}
if(j == grid.length-1){
if(j>0){
if(grid[j][i] != grid[j-1][i]){
res += 1;
}
}
if(grid[j][i] == 1){
res += 1;
}
}
if(j>0){
if((j != 0)&&(j != grid.length-1)){
if(grid[j][i] != grid[j-1][i]){
res += 1;
}
}
}
}
}
return res;
}
public static void main(String[] args) {
// int[][] a= {{0,1,0,0},{1,1,1,0},{0,1,0,0},{1,1,0,0}};
int[][] a= {{1}};
System.out.println(new Solution().islandPerimeter(a));
}
}
相关文章推荐
- LeetCode - 463. Island Perimeter
- leetcode 463. Island Perimeter 周长统计 + 暴力统计即可
- LeetCode 463. Island Perimeter (算法, 矩阵遍历)
- 【LeetCode】 463. Island Perimeter
- LeetCode 463. Island Perimeter (岛的周长)
- leetcode 463. Island Perimeter(C语言,二维数组,周长)23
- LeetCode 463. Island Perimeter
- 【LeetCode】463. Island Perimeter
- Leetcode 463. Island Perimeter
- [Leetcode] 463. Island Perimeter 解题报告
- LeetCode | 463. Island Perimeter
- [leetcode: Python]463. Island Perimeter
- [LeetCode] 463. Island Perimeter 解题报告
- leetcode 463. Island Perimeter
- LeetCode 463. Island Perimeter(Java)
- LeetCode-463. Island Perimeter (Java)
- 【leetcode】【Easy】【463. Island Perimeter】
- leetcode 463. Island Perimeter
- [leetcode]463. Island Perimeter
- Leetcode-463. Island Perimeter