Skip to content

Commit d5e2357

Browse files
add IslandPerimeter
1 parent 53968aa commit d5e2357

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

‎EASY/src/easy/IslandPerimeter.java

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package easy;
2+
3+
public class IslandPerimeter {
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+
public int islandPerimeter(int[][] grid) {
9+
int count = 0;
10+
for (int i = 0; i < grid.length; i++) {
11+
for (int j = 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+
return count;
20+
}
21+
}

‎README.md

+1
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)