Skip to content

Commit 1785d5e

Browse files
refactor 463
1 parent 662b720 commit 1785d5e

File tree

1 file changed

+25
-21
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+25
-21
lines changed
Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.fishercoder.solutions;
22
/**
3-
* You are given a map in form of a two-dimensional integer grid
4-
* where 1 represents land and 0 represents water.
3+
* 463. Island Perimeter
4+
*
5+
* You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represents water.
56
* Grid cells are connected horizontally/vertically (not diagonally).
6-
* The grid is completely surrounded by water,
7-
* and there is exactly one island (i.e., one or more connected land cells).
7+
* The grid is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).
88
* The island doesn't have "lakes" (water inside that isn't connected to the water around the island).
99
* One cell is a square with side length 1.
1010
* The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.
@@ -18,28 +18,32 @@
1818
1919
Answer: 16
2020
Explanation: The perimeter is the 16 yellow stripes in the image below:
21-
2221
*/
2322
public class _463 {
24-
/**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
25-
* 1. we increment the count by 4 whenever we encounter an island
26-
* 2. also, we check in two directions: island's left and island's top, we only check these two directions,
27-
* see if this island has any island neighbors, if so, we'll deduct two from it.*/
28-
public int islandPerimeter(int[][] grid) {
29-
int count = 0;
30-
for (int i = 0; i < grid.length; i++) {
31-
for (int j = 0; j < grid[0].length; j++) {
32-
if (grid[i][j] == 1) {
33-
count += 4;
34-
if (i > 0 && grid[i - 1][j] == 1) {
35-
count -= 2;
36-
}
37-
if (j > 0 && grid[i][j - 1] == 1) {
38-
count -= 2;
23+
24+
public static class Solution1 {
25+
/**
26+
* 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
27+
* 1. we increment the count by 4 whenever we encounter an island
28+
* 2. also, we check in two directions: island's left and island's top, we only check these two directions,
29+
* see if this island has any island neighbors, if so, we'll deduct two from it.
30+
*/
31+
public int islandPerimeter(int[][] grid) {
32+
int count = 0;
33+
for (int i = 0; i < grid.length; i++) {
34+
for (int j = 0; j < grid[0].length; j++) {
35+
if (grid[i][j] == 1) {
36+
count += 4;
37+
if (i > 0 && grid[i - 1][j] == 1) {
38+
count -= 2;
39+
}
40+
if (j > 0 && grid[i][j - 1] == 1) {
41+
count -= 2;
42+
}
3943
}
4044
}
4145
}
46+
return count;
4247
}
43-
return count;
4448
}
4549
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy