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
}
More than 5 years have passed since last update.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme