@Test public void example16() throws Exception { Solution solution = new Solution(); int perimeter = solution.islandPerimeter(new int[][]{ {0, 1, 0, 0}, {1, 1, 1, 0}, {0, 1, 0, 0}, {1, 1, 0, 0} }); assertThat(perimeter, is(16)); }
public int islandPerimeter(int[][] grid) { int totalRows = grid.length; if (totalRows == 0) { return 0; } else { int perimeter = 0; int totalColumns = grid[0].length; for (int i = 0; i < totalRows; i++) { for (int j = 0; j < totalColumns; j++) { if (isLand(i, j, grid, totalRows, totalColumns)) { perimeter += (4 - neighborLands(i, j, grid, totalRows, totalColumns)); } } } return perimeter; } }
Solution { public int islandPerimeter(int[][] grid) { int totalRows = grid.length; if (totalRows == 0) { return 0; } else { int perimeter = 0; int totalColumns = grid[0].length; for (int i = 0; i < totalRows; i++) { for (int j = 0; j < totalColumns; j++) { if (isLand(i, j, grid, totalRows, totalColumns)) { perimeter += (4 - neighborLands(i, j, grid, totalRows, totalColumns)); } } } return perimeter; } } }
Solution { public int islandPerimeter(int[][] grid) { int totalRows = grid.length; if (totalRows == 0) { return 0; } else { int perimeter = 0; int totalColumns = grid[0].length; for (int i = 0; i < totalRows; i++) { for (int j = 0; j < totalColumns; j++) { if (isLand(i, j, grid, totalRows, totalColumns)) { perimeter += (4 - neighborLands(i, j, grid, totalRows, totalColumns)); } } } return perimeter; } }  }
Solution { public int islandPerimeter(int[][] grid) { int totalRows = grid.length; if (totalRows == 0) { return 0; } else { int perimeter = 0; int totalColumns = grid[0].length; for (int i = 0; i < totalRows; i++) { for (int j = 0; j < totalColumns; j++) { if (isLand(i, j, grid, totalRows, totalColumns)) { perimeter += (4 - neighborLands(i, j, grid, totalRows, totalColumns)); } } } return perimeter; } }  int islandPerimeter(int[][] grid); }
Solution { public int islandPerimeter(int[][] grid) { int totalRows = grid.length; if (totalRows == 0) { return 0; } else { int perimeter = 0; int totalColumns = grid[0].length; for (int i = 0; i < totalRows; i++) { for (int j = 0; j < totalColumns; j++) { if (isLand(i, j, grid, totalRows, totalColumns)) { perimeter += (4 - neighborLands(i, j, grid, totalRows, totalColumns)); } } } return perimeter; } }  int islandPerimeter(int[][] grid);  }