AC Java Solution with 4 Conditions


  • 0
    K
    public class Solution {
        public int countBattleships(char[][] board) {
            int count = 0;
            for(int i=0;i<board.length;i++) {
                for(int j=0;j<board[0].length;j++) {
                    if(board[i][j] == '.' 
                    || (i-1 >= 0 && j-1 >= 0 && (board[i-1][j] == 'X' || board[i][j-1] =='X'))
                    || (i == 0 && j > 0 && board[i][j-1] == 'X')
                    || (j == 0 && i > 0 && board[i-1][j] == 'X')
                    ) continue;
                    count++;
                }
            }
            return count;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.