LeetCode "419. Battleships in a Board"

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 time, by problem statement. For horizontal, we simply stripe through right, and plus 1 - however, if our top element is ‘X‘ already, it is a vertical and counter has alread been increased.

class Solution {
public:
    int countBattleships(vector<vector<char>>& board) {
        int h = board.size();
        if (!h) return 0;
        int w = board[0].size();
        if (!w) return 0;

        int cnt = 0;
        for(int i = 0; i < h; i ++)
        for(int j = 0; j < w; j ++)
        {
            if(board[i][j] == ‘X‘)
            {
                // is it a counted vertical case?
                if(!(i > 0 && board[i -1][j] == ‘X‘))
                {
                    cnt ++;
                    // Horizontal
                    while(j < (w - 1) && board[i][j + 1] == ‘X‘) j ++;
                }
            }
        }

        return cnt;
    }
};
时间: 2024-10-26 03:15:51

LeetCode "419. Battleships in a Board"的相关文章

[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

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

419 Battleships in a Board 甲板上的战舰

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

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 419

题目说明: 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 empt

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

Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)

Sorted by frequency of problems that appear in real interviews.Last updated: October 2, 2017Google (214)534 Design TinyURL388 Longest Absolute File Path683 K Empty Slots340 Longest Substring with At Most K Distinct Characters681 Next Closest Time482

过中等难度题目.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.