Skip to content

Commit e72efb5

Browse files
committed
🎃(array): 463. 岛屿的周长 add java code
1 parent 1818255 commit e72efb5

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

docs/data-structure/array/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1732,6 +1732,31 @@ func islandPerimeter(grid [][]int) int {
17321732
}
17331733
```
17341734

1735+
#### **Java**
1736+
1737+
```java
1738+
class Solution {
1739+
public int islandPerimeter(int[][] grid) {
1740+
if (grid == null || grid.length <= 0 || grid[0] == null || grid[0].length <= 0) return 0;
1741+
int height = grid.length;
1742+
int width = grid[0].length;
1743+
1744+
int result = 0;
1745+
1746+
for (int i = 0; i < height; i++) {
1747+
for (int j = 0; j < width; j++) {
1748+
if (grid[i][j] == 1) {
1749+
if (i == 0 || grid[i - 1][j] == 0) result++;
1750+
if (j == 0 || grid[i][j - 1] == 0) result++;
1751+
}
1752+
}
1753+
}
1754+
1755+
return result * 2;
1756+
}
1757+
}
1758+
```
1759+
17351760
<!-- tabs:end -->
17361761

17371762
## 485. 最大连续1的个数

0 commit comments

Comments
 (0)