@Test public void twoRowShips() throws Exception { char[][] board = new char[][]{ {'X', 'X', '.'}, {'.', '.', '.'}, {'.', 'X', 'X'} }; int count = new Solution().countBattleships(board); assertThat(count, is(2)); }
public int countBattleships(char[][] board) { int count = 0; rows = board.length; columns = board[0].length; for (int row = 0; row < rows; row++) { for (int column = 0; column < columns; column++) { char symbol = board[row][column]; if (symbol == X && !(!(row < 0 || row >= rows || column - 1 < 0 || column - 1 >= columns) && board[row][column - 1] == X || !(row - 1 < 0 || row - 1 >= rows || column < 0 || column >= columns) && board[row - 1][column] == X)) { count++; } } } return count; }
Solution { public int countBattleships(char[][] board) { int count = 0; rows = board.length; columns = board[0].length; for (int row = 0; row < rows; row++) { for (int column = 0; column < columns; column++) { char symbol = board[row][column]; if (symbol == X && !(!(row < 0 || row >= rows || column - 1 < 0 || column - 1 >= columns) && board[row][column - 1] == X || !(row - 1 < 0 || row - 1 >= rows || column < 0 || column >= columns) && board[row - 1][column] == X)) { count++; } } } return count; } }
Solution { public int countBattleships(char[][] board) { int count = 0; rows = board.length; columns = board[0].length; for (int row = 0; row < rows; row++) { for (int column = 0; column < columns; column++) { char symbol = board[row][column]; if (symbol == X && !(!(row < 0 || row >= rows || column - 1 < 0 || column - 1 >= columns) && board[row][column - 1] == X || !(row - 1 < 0 || row - 1 >= rows || column < 0 || column >= columns) && board[row - 1][column] == X)) { count++; } } } return count; }  }
Solution { public int countBattleships(char[][] board) { int count = 0; rows = board.length; columns = board[0].length; for (int row = 0; row < rows; row++) { for (int column = 0; column < columns; column++) { char symbol = board[row][column]; if (symbol == X && !(!(row < 0 || row >= rows || column - 1 < 0 || column - 1 >= columns) && board[row][column - 1] == X || !(row - 1 < 0 || row - 1 >= rows || column < 0 || column >= columns) && board[row - 1][column] == X)) { count++; } } } return count; }  int countBattleships(char[][] board); }
Solution { public int countBattleships(char[][] board) { int count = 0; rows = board.length; columns = board[0].length; for (int row = 0; row < rows; row++) { for (int column = 0; column < columns; column++) { char symbol = board[row][column]; if (symbol == X && !(!(row < 0 || row >= rows || column - 1 < 0 || column - 1 >= columns) && board[row][column - 1] == X || !(row - 1 < 0 || row - 1 >= rows || column < 0 || column >= columns) && board[row - 1][column] == X)) { count++; } } } return count; }  int countBattleships(char[][] board); static final char X; }