Skip to content

Commit 39a9e92

Browse files
authored
feat: add solutions to lc problem: No.0463 (#603)
No.0463.Island Perimeter
1 parent fbfd3dc commit 39a9e92

File tree

4 files changed

+102
-4
lines changed

4 files changed

+102
-4
lines changed

solution/0400-0499/0463.Island Perimeter/README.md

+34-2
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,47 @@
6262
<!-- 这里可写当前语言的特殊实现逻辑 -->
6363

6464
```python
65-
65+
class Solution:
66+
def islandPerimeter(self, grid: List[List[int]]) -> int:
67+
m, n = len(grid), len(grid[0])
68+
ans = 0
69+
for i in range(m):
70+
for j in range(n):
71+
if grid[i][j] == 1:
72+
ans += 4
73+
if i < m - 1 and grid[i + 1][j] == 1:
74+
ans -= 2
75+
if j < n -1 and grid[i][j + 1] == 1:
76+
ans -= 2
77+
return ans
6678
```
6779

6880
### **Java**
6981

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

7284
```java
73-
85+
class Solution {
86+
public int islandPerimeter(int[][] grid) {
87+
int ans = 0;
88+
int m = grid.length;
89+
int n = grid[0].length;
90+
for (int i = 0; i < m; i++) {
91+
for (int j = 0; j < n; j++) {
92+
if (grid[i][j] == 1) {
93+
ans += 4;
94+
if (i < m - 1 && grid[i + 1][j] == 1) {
95+
ans -= 2;
96+
}
97+
if (j < n - 1 && grid[i][j + 1] == 1) {
98+
ans -= 2;
99+
}
100+
}
101+
}
102+
}
103+
return ans;
104+
}
105+
}
74106
```
75107

76108
### **TypeScript**

solution/0400-0499/0463.Island Perimeter/README_EN.md

+34-2
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,45 @@
5151
### **Python3**
5252

5353
```python
54-
54+
class Solution:
55+
def islandPerimeter(self, grid: List[List[int]]) -> int:
56+
m, n = len(grid), len(grid[0])
57+
ans = 0
58+
for i in range(m):
59+
for j in range(n):
60+
if grid[i][j] == 1:
61+
ans += 4
62+
if i < m - 1 and grid[i + 1][j] == 1:
63+
ans -= 2
64+
if j < n -1 and grid[i][j + 1] == 1:
65+
ans -= 2
66+
return ans
5567
```
5668

5769
### **Java**
5870

5971
```java
60-
72+
class Solution {
73+
public int islandPerimeter(int[][] grid) {
74+
int ans = 0;
75+
int m = grid.length;
76+
int n = grid[0].length;
77+
for (int i = 0; i < m; i++) {
78+
for (int j = 0; j < n; j++) {
79+
if (grid[i][j] == 1) {
80+
ans += 4;
81+
if (i < m - 1 && grid[i + 1][j] == 1) {
82+
ans -= 2;
83+
}
84+
if (j < n - 1 && grid[i][j + 1] == 1) {
85+
ans -= 2;
86+
}
87+
}
88+
}
89+
}
90+
return ans;
91+
}
92+
}
6193
```
6294

6395
### **TypeScript**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public int islandPerimeter(int[][] grid) {
3+
int ans = 0;
4+
int m = grid.length;
5+
int n = grid[0].length;
6+
for (int i = 0; i < m; i++) {
7+
for (int j = 0; j < n; j++) {
8+
if (grid[i][j] == 1) {
9+
ans += 4;
10+
if (i < m - 1 && grid[i + 1][j] == 1) {
11+
ans -= 2;
12+
}
13+
if (j < n - 1 && grid[i][j + 1] == 1) {
14+
ans -= 2;
15+
}
16+
}
17+
}
18+
}
19+
return ans;
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def islandPerimeter(self, grid: List[List[int]]) -> int:
3+
m, n = len(grid), len(grid[0])
4+
ans = 0
5+
for i in range(m):
6+
for j in range(n):
7+
if grid[i][j] == 1:
8+
ans += 4
9+
if i < m - 1 and grid[i + 1][j] == 1:
10+
ans -= 2
11+
if j < n -1 and grid[i][j + 1] == 1:
12+
ans -= 2
13+
return ans

0 commit comments

Comments
 (0)