[LeetCode]419. Battleships in a Board

来源:互联网 发布:windows xp企业版下载 编辑:程序博客网 时间:2024/05/16 03:00

https://leetcode.com/problems/battleships-in-a-board/?tab=Description

找出连续X(横向或者纵向)的个数





public class Solution {    public int countBattleships(char[][] board) {        if (board == null || board.length == 0) {            return 0;        }        int res = 0;        for (int i = 0; i < board.length; i++) {            for (int j = 0; j < board[0].length; j++) {                if (board[i][j] == '.') {                    continue;                }                if (i > 0 && board[i - 1][j] == 'X') {                    continue;                }                if (j > 0 && board[i][j - 1] == 'X') {                    continue;                }                res++;            }        }        return res;    }}


0 0
原创粉丝点击