题目链接:463. 岛屿的周长 - 力扣(LeetCode)
题目描述
我的代码实现
class Solution {public int islandPerimeter(int[][] grid) {
int result=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){//判断上//如果是最上面一行直接加1if(i==0){ result++;}else{//如果不是判断它上面if(grid[i-1][j]!=1){result++;}}//判断下//如果是最下面一行直接加一if(i==row-1){result++;}else{//如果是其他行判断一下if(grid[i+1][j]!=1){result++;}}//判断左//如果是最左边if(j==0){result++;}else{if(grid[i][j-1]!=1){result++;}}//判断右if(j==col-1){result++;}else{if(grid[i][j+1]!=1){result++;}}}}
}return result;}
}