-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path419.py
35 lines (30 loc) · 1.05 KB
/
419.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/env python
# coding=utf-8
class Solution(object):
def countBattleships(self, board):
"""
:type board: List[List[str]]
:rtype: int
"""
count = 0
for i in range(len(board)):
for j in range(len(board[i])):
if board[i][j] == 'X':
count += 1
k = i-1
while k>=0 and board[k][j] == 'X':
board[k][j] = '.'
k = k - 1
k = i+1
while k<len(board) and board[k][j] == 'X':
board[k][j] = '.'
k = k + 1
k = j-1
while k>=0 and board[i][k] == 'X':
board[i][k] = '.'
k = k - 1
k = j+1
while k<len(board[i]) and board[i][k] == 'X':
board[i][k] = '.'
k = k + 1
return count