您的位置:首页 > 其它

battleships-in-a-board

2016-10-16 14:15 330 查看
https://leetcode.com/problems/battleships-in-a-board/

// 采用的是排除法,看船的最后一个节点

public class Solution {
public int countBattleships(char[][] board) {
int rows = board.length;
int cols = rows > 0 ? board[0].length : 0;
if (rows == 0 || cols == 0) {
return 0;
}

int ret = 0;
for (int i=0; i<rows; i++) {
for (int j=0; j<cols; j++) {
if (board[i][j] == 'X') {
if ((i+1>=rows || board[i+1][j]=='.') &&
(j+1>=cols || board[i][j+1]=='.')) {
ret++;
}
}
}
}
return ret;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: