leetcode oj java Battleships in a Board
2016-11-22 21:03
316 查看
一、问题描述:
Given an 2D board, count how many different battleships are in it. The battleships are represented with
empty slots are represented with
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
row, N columns) or
At least one horizontal or vertical cell separates between two battleships - there are no adjacent battleships.
Example:
In the above board there are 2 battleships.
Invalid Example:
This is an invalid board that you will not receive - as battleships will always have a cell separating between them.
二、解决方案:
要求任意两个战舰之间不会近邻,那么只需要找到战舰的头元素就可以了。头元素必须保证 tmp[i][j] == 'X' && tmp[i - 1][j] == '.' && tmp[i][j
- 1] == '.' ,即本身为'X'但是左方和上方都为'.' 。 否则即为空的分割点或者是战舰的中间元素。我们新建一个tmp数组保存board,在最上边和最左边各加一列'.'。 满足one-pass的要求。
三、代码:
public class Solution {
public int countBattleships(char[][] board) {
int re = 0;
int l = board.length;
int w = board[0].length;
char[][] tmp = new char[l + 1][w + 1];
for (int i = 0; i < l; i++) {
for (int j = 0; j < w; j++) {
tmp[i + 1][j + 1] = board[i][j];
}
}
for (int i = 0; i < w + 1; i++) {
tmp[0][i] = '.';
}
for (int i = 0; i < l + 1; i++) {
tmp[i][0] = '.';
}
for (int i = 1; i < l+1; i++) {
for (int j = 1; j < w + 1; j++) {
if (tmp[i][j] == 'X' && tmp[i - 1][j] == '.' && tmp[i][j - 1] == '.')
re++;
}
}
return re;
}
}
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 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.
二、解决方案:
要求任意两个战舰之间不会近邻,那么只需要找到战舰的头元素就可以了。头元素必须保证 tmp[i][j] == 'X' && tmp[i - 1][j] == '.' && tmp[i][j
- 1] == '.' ,即本身为'X'但是左方和上方都为'.' 。 否则即为空的分割点或者是战舰的中间元素。我们新建一个tmp数组保存board,在最上边和最左边各加一列'.'。 满足one-pass的要求。
三、代码:
public class Solution {
public int countBattleships(char[][] board) {
int re = 0;
int l = board.length;
int w = board[0].length;
char[][] tmp = new char[l + 1][w + 1];
for (int i = 0; i < l; i++) {
for (int j = 0; j < w; j++) {
tmp[i + 1][j + 1] = board[i][j];
}
}
for (int i = 0; i < w + 1; i++) {
tmp[0][i] = '.';
}
for (int i = 0; i < l + 1; i++) {
tmp[i][0] = '.';
}
for (int i = 1; i < l+1; i++) {
for (int j = 1; j < w + 1; j++) {
if (tmp[i][j] == 'X' && tmp[i - 1][j] == '.' && tmp[i][j - 1] == '.')
re++;
}
}
return re;
}
}
相关文章推荐
- LeetCode 419 Battleships in a Board (DFS 或 模拟)
- leetcode oj java Find Minimum in Rotated Sorted Array
- LeetCode OJ平台上Binary Tree Inorder Traversal题目使用java堆栈方式实现
- leetcode oj java Find All Anagrams in a String
- leetcode oj java Number of Segments in a String
- Battleships in a Board [Leetcode]
- leetcode419 Battlesships In A Board JAVA
- [LeetCode] Battleships in a Board 平板上的战船
- Leetcode oj java Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode Battleships in a Board
- Leetcode 419 Battleships in a Board
- [LeetCode]Battleships in a Board 战舰数目
- Leetcode oj java Delete Node in a Linked List
- leetcode 419 Battleships in a Board C++
- LeetCode #419: Battleships in a Board
- LeetCode:419. Battleships in a Board
- LeetCode[419] Battleships in a Board
- Search in Rotated Sorted Array II leetcode java
- Leetcode Solution of Longest Palindromic Substring in Java
- LeetCode 33 — Search in Rotated Sorted Array(C++ Java Python)