码迷,mamicode.com
首页 > 其他好文 > 详细

battleships-in-a-board

时间:2016-10-16 16:07:23      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

 

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;
    }
}

 

battleships-in-a-board

标签:

原文地址:http://www.cnblogs.com/charlesblc/p/5966570.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!