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

 

相关文章:

  • 2021-07-13
  • 2021-04-29
  • 2022-12-23
  • 2021-09-09
  • 2022-12-23
  • 2021-09-21
  • 2021-07-25
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-11-23
  • 2022-12-23
  • 2022-03-02
  • 2021-10-29
  • 2022-12-23
相关资源
相似解决方案