Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd5e2357

Browse files
add IslandPerimeter
1 parent53968aa commitd5e2357

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

‎EASY/src/easy/IslandPerimeter.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
packageeasy;
2+
3+
publicclassIslandPerimeter {
4+
/**Inspired by this post: https://discuss.leetcode.com/topic/68983/java-9-line-solution-add-4-for-each-land-and-remove-2-for-each-internal-edge
5+
* 1. we increment the count by 4 whenever we encounter an island
6+
* 2. also, we check in two directions: island's left and island's top, we only check these two directions,
7+
* see if this island has any island neighbors, if so, we'll deduct two from it.*/
8+
publicintislandPerimeter(int[][]grid) {
9+
intcount =0;
10+
for (inti =0;i <grid.length;i++) {
11+
for (intj =0;j <grid[0].length;j++) {
12+
if(grid[i][j] ==1) {
13+
count +=4;
14+
if(i >0 &&grid[i-1][j] ==1)count -=2;
15+
if(j >0 &&grid[i][j-1] ==1)count -=2;
16+
}
17+
}
18+
}
19+
returncount;
20+
}
21+
}

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#fishercoderLeetcode
22
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
33
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
4+
|463|[Island Perimeter](https://leetcode.com/problems/island-perimeter/)|[Solution](../../blob/master/EASY/src/easy/IslandPerimeter.java)| O(m*n)|O(1)| Easy|
45
|459|[Repeated Substring Pattern](https://leetcode.com/problems/repeated-substring-pattern/)|[Solution](../../blob/master/EASY/src/easy/RepeatedSubstringPattern.java)| O(n)|O(n) | Easy| KMP
56
|456|[132 Pattern](https://leetcode.com/problems/132-pattern/)|[Solution](../../blob/master/MEDIUM/src/medium/_132Pattern.java) | O(n) |O(n) | Medium| Stack
67
|455|[Assign Cookies](https://leetcode.com/problems/assign-cookies/)|[Solution](../../blob/master/EASY/src/easy/AssignCookies.java)| O(n)|O(1)| Easy|

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp