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 rules:

  • You receive a valid board, made of only battleships or empty slots.
  • Battleships can only be placed horizontally or vertically. In other words, they can only be made of the shape 1xN (1 row, N columns) or Nx1 (N rows, 1 column), where N can be of any size.
  • At least one horizontal or vertical cell separates between two battleships - there are no adjacent battleships.

Example:

X..X
...X
...X

In the above board there are 2 battleships.

Invalid Example:

...X
XXXX
...X

This is an invalid board that you will not receive - as battleships will always have a cell separating between them.

Follow up:
Could you do it in one-pass, using only O(1) extra memory and without modifying the value of the board?

题解:

无论是1*N 还是 N*1型的battleship, 都以左上角的X来标志. 只有遇到X, 并且左边或者上边没有连着的X时才算新的battleship.

Time Complexity: O(mn), m = board.length, n = board[0].length.

Space: O(1).

AC Java:

 1 public class Solution {
 2     public int countBattleships(char[][] board) {
 3         if(board == null || board.length == 0 || board[0].length == 0){
 4             return 0;
 5         }
 6
 7         int res = 0;
 8         for(int i = 0; i<board.length; i++){
 9             for(int j = 0; j<board[0].length; j++){
10                 if(board[i][j] == ‘.‘){
11                     continue;
12                 }
13                 if(i>0 && board[i-1][j]==‘X‘){
14                     continue;
15                 }
16                 if(j>0 && board[i][j-1]==‘X‘){
17                     continue;
18                 }
19                 res++;
20             }
21         }
22         return res;
23     }
24 }
时间: 2025-01-23 13:28:14

LeetCode Battleships in a Board的相关文章

[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 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(

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

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】未分类(tag里面没有)(共题)

[419]Battleships in a Board (2018年11月25日)(谷歌的题,没分类.) 给了一个二维平面,上面有 X 和 . 两种字符. 一行或者一列连续的 X 代表一个战舰,问图中有多少个战舰.(题目要求one pass, 空间复杂度是常数) 题目说了每两个战舰之间起码有一个 . 作为分隔符,所以不存在正好交叉的情况. 题解:我提交了一个 floodfill 的题解,能过但是显然不满足要求. discuss里面说,我们可以统计战舰的最上方和最左方,从而来统计战舰的个数.(这个

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

LeetCode 0079. Word Search单词搜索【Python】

LeetCode 0079. Word Search单词搜索[Medium][Python][DFS] Problem LeetCode Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horiz