battleships-in-a-board

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;
    }
}
时间: 2024-08-10 14:22:46

battleships-in-a-board的相关文章

419. Battleships in a Board

Given an 2D board, count how many different battleships are in it. The battleships are represented with 'X's, empty slots are represented with '.'s. You may assume the following rules: You receive a valid board, made of only battleships or empty slot

LeetCode Battleships in a Board

原题链接在这里:https://leetcode.com/problems/battleships-in-a-board/#/description 题目: Given an 2D board, count how many battleships are in it. The battleships are represented with 'X's, empty slots are represented with '.'s. You may assume the following rul

[LeetCode] Battleships in a Board 平板上的战船

Given an 2D board, count how many different battleships are in it. The battleships are represented with 'X's, empty slots are represented with '.'s. You may assume the following rules: You receive a valid board, made of only battleships or empty slot

LeetCode &quot;419. Battleships in a Board&quot;

The follow-up question is fun: "Could you do it in one-pass, using only O(1) extra memory and without modifying the value of the board?" When we meet an 'X', we need to check if it is vertical or horizontal: they will never happen at the same ti

[LeetCode]419 Battleships in a Board(暴力,dfs)

题目链接:https://leetcode.com/problems/battleships-in-a-board/?tab=Description 题意:问平面上有多少条船,船是1*n或者n*1的,并且不相交. 最简单的想法就是直接dfs,但是看到题目里有一个思考,那就是不修改平面并且空间O(1)的one-pass. 这时候就想到应该是遍历所有船的左上角的端点,统计就行了.很简单. 1 public class Solution { 2 public int countBattleships(

419 Battleships in a Board 甲板上的战舰

给定一个二维的甲板, 请计算其中有多少艘战舰. 战舰用 'X'表示,空位用 '.'表示. 你需要遵守以下规则:    给你一个有效的甲板,仅由战舰或者空位组成.    战舰只能水平或者垂直放置.换句话说,战舰只能由 1xN (1 行, N 列)组成,或者 Nx1 (N 行, 1 列)组成,其中N可以是任意大小.    两艘战舰之间至少有一个水平或垂直的空位分隔 - 即没有相邻的战舰.示例 :X..X...X...X在上面的甲板中有2艘战舰.无效样例 :...XXXXX...X你不会收到这样的无效

过中等难度题目.0310

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.

战舰数量

版权声明: 本文由Faye_Zuo发布于http://www.cnblogs.com/zuofeiyi/, 本文可以被全部的转载或者部分使用,但请注明出处. 上周经历了人生中最艰辛的几天,所以刷题又耽误了3周多,今天重新开刷.又出现恍如隔世的感觉了! 来看看这道题: Battleships in a Board(419) 一.我的思路: 1.初始化一个二维数组: 2.然后开始找字符x; 3.如果找到x,找它的邻居. if(up down && no-left no-right) count

继续过中等难度.0309

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea