swift solution


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

Log in to reply
 

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