Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions solution/0400-0499/0463.Island Perimeter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,47 @@
<!-- 这里可写当前语言的特殊实现逻辑 -->

```python

class Solution:
def islandPerimeter(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
ans = 0
for i in range(m):
for j in range(n):
if grid[i][j] == 1:
ans += 4
if i < m - 1 and grid[i + 1][j] == 1:
ans -= 2
if j < n -1 and grid[i][j + 1] == 1:
ans -= 2
return ans
```

### **Java**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```java

class Solution {
public int islandPerimeter(int[][] grid) {
int ans = 0;
int m = grid.length;
int n = grid[0].length;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 1) {
ans += 4;
if (i < m - 1 && grid[i + 1][j] == 1) {
ans -= 2;
}
if (j < n - 1 && grid[i][j + 1] == 1) {
ans -= 2;
}
}
}
}
return ans;
}
}
```

### **TypeScript**
Expand Down
36 changes: 34 additions & 2 deletions solution/0400-0499/0463.Island Perimeter/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,45 @@
### **Python3**

```python

class Solution:
def islandPerimeter(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
ans = 0
for i in range(m):
for j in range(n):
if grid[i][j] == 1:
ans += 4
if i < m - 1 and grid[i + 1][j] == 1:
ans -= 2
if j < n -1 and grid[i][j + 1] == 1:
ans -= 2
return ans
```

### **Java**

```java

class Solution {
public int islandPerimeter(int[][] grid) {
int ans = 0;
int m = grid.length;
int n = grid[0].length;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 1) {
ans += 4;
if (i < m - 1 && grid[i + 1][j] == 1) {
ans -= 2;
}
if (j < n - 1 && grid[i][j + 1] == 1) {
ans -= 2;
}
}
}
}
return ans;
}
}
```

### **TypeScript**
Expand Down
21 changes: 21 additions & 0 deletions solution/0400-0499/0463.Island Perimeter/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public int islandPerimeter(int[][] grid) {
int ans = 0;
int m = grid.length;
int n = grid[0].length;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 1) {
ans += 4;
if (i < m - 1 && grid[i + 1][j] == 1) {
ans -= 2;
}
if (j < n - 1 && grid[i][j + 1] == 1) {
ans -= 2;
}
}
}
}
return ans;
}
}
13 changes: 13 additions & 0 deletions solution/0400-0499/0463.Island Perimeter/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution:
def islandPerimeter(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
ans = 0
for i in range(m):
for j in range(n):
if grid[i][j] == 1:
ans += 4
if i < m - 1 and grid[i + 1][j] == 1:
ans -= 2
if j < n -1 and grid[i][j + 1] == 1:
ans -= 2
return ans