Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. class Solution {
  2. public int islandPerimeter(int[][] grid) {
  3.  
  4. int [][] island = new int[4][4];
  5. island[0] = new int[]{0, 1, 0, 0};
  6. island[1] = new int[]{1, 1, 1, 0};
  7. island[2] = new int[]{0, 1, 0, 0};
  8. island[3] = new int[]{1, 1, 0, 0};
  9.  
  10. int count = 0;
  11. for (int i = 0; i < island.length; i++) {
  12. for (int j = 0; j < island[0].length; j++) {
  13. if (island[i][j] == 1) {
  14. count += 4;
  15.  
  16. if (j > 0 && island[i][j - 1] == 1) {
  17. count--;
  18. }
  19. if (j < island[0].length - 1 && island[i][j + 1] == 1) {
  20. count--;
  21. }
  22. if (i > 0 && island[i - 1][j] == 1) {
  23. count--;
  24. }
  25. if (i < island.length - 1 && island[i + 1][j] == 1) {
  26. count--;
  27. }
  28. }
  29.  
  30. }
  31. }
  32.  
  33. return count;
  34.  
  35. }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement