solution 1 for c++, O(n^2) and memory O(1)


  • 0
    J

    Soluation:

    class Solution {
    public:
        int countBattleships(vector<vector<char>>& board) {
            int row = board.size();
            if (0== row) {
                return 0;
            }
            int col = board[0].size();
            int count = 0;
    
            for(int i = 0; i < row; i++) {
                for(int j = 0; j < col; j++) {
                    if (board[i][j] == 'X' && 
                     (i == 0 || board[i-1][j] ==  '.' ) && 
                     (j == 0 || board[i][j-1] == '.')){
                        count++;
                    }
                }
            }
            return count;  
        }
    };
    

Log in to reply
 

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