LoginSignup
0
0

More than 3 years have passed since last update.

Leetcode #419: Battleships in a Board

Last updated at Posted at 2019-07-02
func countBattleships(_ board: [[Character]]) -> Int {
    if board.isEmpty || board[0].isEmpty {
        return 0
    }
    var answer = 0
    for i in stride(from: 0, to: board.count, by: 1) {
        for j in stride(from: 0, to: board[0].count, by: 1) {
            if board[i][j] == "." {
                continue
            }
            if i > 0 && board[i-1][j] == "X" {
                continue
            }
            if j > 0 && board[i][j-1] == "X" {
                continue
            }
            answer += 1
        }
    }
    return answer
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0