Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public int islandPerimeter(int[][] grid) {
- int [][] island = new int[4][4];
- island[0] = new int[]{0, 1, 0, 0};
- island[1] = new int[]{1, 1, 1, 0};
- island[2] = new int[]{0, 1, 0, 0};
- island[3] = new int[]{1, 1, 0, 0};
- int count = 0;
- for (int i = 0; i < island.length; i++) {
- for (int j = 0; j < island[0].length; j++) {
- if (island[i][j] == 1) {
- count += 4;
- if (j > 0 && island[i][j - 1] == 1) {
- count--;
- }
- if (j < island[0].length - 1 && island[i][j + 1] == 1) {
- count--;
- }
- if (i > 0 && island[i - 1][j] == 1) {
- count--;
- }
- if (i < island.length - 1 && island[i + 1][j] == 1) {
- count--;
- }
- }
- }
- }
- return count;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement