Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public int islandPerimeter(int[][] grid) {
- int[][] neighbors = {
- {-1, 0},
- {0, -1},
- {0, 1},
- {1, 0}
- };
- int perimeter = 0;
- for (int x = 0; x < grid.length; x++) {
- for (int y = 0; y < grid[x].length; y++) {
- if (grid[x][y] == 0) continue;
- for (int[] neighbor : neighbors) {
- try {
- if (grid[x + neighbor[0]][y + neighbor[1]] == 0) perimeter++;
- } catch (ArrayIndexOutOfBoundsException exception) {
- perimeter++;
- }
- }
- }
- }
- return perimeter;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement