c++ solution


  • 0
    B
    class Solution {
    public:
        int countBattleships(vector<vector<char>>& board) {
            int result=0;
            if(!board.size()) return result;
            
            for(int i=0;i<board.size();i++){
                for(int j=0;j<board[0].size();j++){
                    if(board[i][j]=='X'){
                        result++;
                        //top or left is part of ship
                        if((i>0 && board[i-1][j]=='X') || (j>0 && board[i][j-1]=='X')) 
                            result--;
                    }
                }
            }
            
            return result;
        }
    };
    

Log in to reply
 

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