---
# System prepended metadata

title: 0463. Island Perimeter
tags: [Leetcode, FaceBook, Easy, Island]

---

# 0463. Island Perimeter
###### tags: `Leetcode` `Easy` `FaceBook` `Island`
Link: https://leetcode.com/problems/island-perimeter/
## 思路
对于每一个land，先加4，如果它上面是一个land，就减2，因为在上下方向的两条边全都不需要，如果左边是一个land，也同理
## Code
```java=
class Solution {
    public int islandPerimeter(int[][] grid) {
        int retVal = 0;
        for(int i = 0;i < grid.length;i++){
            for(int j = 0;j < grid[0].length;j++){
                if(grid[i][j] == 1){
                    retVal += 4;
                    if(i!=0 && grid[i-1][j]==1){
                        retVal-=2;
                    }
                    if(j!=0 && grid[i][j-1]==1){
                        retVal-=2;
                    }   
                }
            }
        }
        return retVal;
    }
}
```