Swift solution


  • 0
    X
    class Solution {
        func countBattleships(_ board: [[Character]]) -> Int {
            var counter: Int = 0
            for i in 0..<board.count {
                for j in 0..<board[i].count {
                    if ( (board[i][j] == "X") && (i < 1 || board[i-1][j] == ".") && (j < 1 || board[i][j-1] == ".") ) {
                        counter += 1
                    }
                }
            }
            return counter
        }
    }
    

Log in to reply
 

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