问题描述
问题链接: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?
我的代码
思路探索
开始的时候看到这个题,我就开始想深度优先/广度优先遍历了,正在思索的时候看到了Follow Up的问题。它问能不能不修改棋盘的内容。我就在想为什么要修改棋盘的内容。
思考了一会儿,我豁然开朗,只要每艘船留下一个”X”就可以了,剩下的X都可以变成.省得混淆视听。
通过的代码
public class Solution {
public int countBattleships(char[][] board) {
/*
思路是这样的,遍历棋盘,如果碰到一个x,就开始进行处理,把这艘战舰除了碰到的这个x以外都变成.
遍历完成后,再数一下棋盘上就几个x即可得到结果
*/
int m = board.length; // 长度
int n = board[0].length; // 宽度
int count = 0;
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(board[i][j] == 'X'){
eraseShip(board,i,j);
count++;
}
}
}
return count;
}
private void eraseShip(char[][] board,int x,int y){
int m = board.length; // 长度
int n = board[0].length; // 宽度
int i = 1;
int j = 1;
// 纵向删除
while(true){
if(x + i >= m || board[x + i][y] == '.'){
break;
}else{
board[x + i][y] = '.';
i++;
}
}
// 横向删除
while(true){
if(y + j >= n || board[x][y + j] == '.'){
break;
}else{
board[x][y + j] = '.';
j++;
}
}
}
}
我的代码打败了40.02%的Java代码。
那个Follow Up里面的要求我没想出来,再到讨论区学习一下吧。
讨论区
果然讨论区大神多啊,瞬间学会了不修改,只遍历一遍的方法。
Simple Java Solution
链接地址:https://discuss.leetcode.com/topic/62970/simple-java-solution
Going over all cells, we can count only those that are the “first” cell of the battleship. First cell will be defined as the most top-left cell. We can check for first cells by only counting cells that do not have an ‘X’ to the left and do not have an ‘X’ above them.
大体的思路是属战舰的”第一个点”,定义为左上角的那个X。判别方法是这个X的左边没有X,上面也没有X。
代码如下:
public int countBattleships(char[][] board) {
int m = board.length;
if (m==0) return 0;
int n = board[0].length;
int count=0;
for (int i=0; i<m; i++) {
for (int j=0; j<n; j++) {
if (board[i][j] == '.') continue;
if (i > 0 && board[i-1][j] == 'X') continue;
if (j > 0 && board[i][j-1] == 'X') continue;
count++;
}
}
return count;
}
Share my 7-line code, 1-line core code, 3ms, super easy
链接地址:https://discuss.leetcode.com/topic/64027/share-my-7-line-code-1-line-core-code-3ms-super-easy
这个的思路和上面的基本一致,写的更简洁,来欣赏一下。
public int countBattleships(char[][] board) {
int count = 0;
for(int i=0;i<board.length;i++)
for(int j=0;j<board[0].length;j++)
if(board[i][j]=='X' && (i==0 || board[i-1][j]!='X') && (j==0 || board[i][j-1]!='X')) count++;
return count;
}