题解——Leetcode 419. Battleships in a Board 难度:Medium
2017-02-24 22:17
549 查看
Given an 2D board, count how many battleships are in it. The battleships 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
At least one horizontal or vertical cell separates between two battleships - there are no adjacent battleships.
Could you do it in one-pass, using only
O(1) extra memory and without modifying the value of the board?
题目如上,简化后的题意如下:
给你一个方形的板,上面分布着战舰和狭槽,其中战舰用‘X’表示,狭槽用‘.’表示,但战舰的位置有一定限制。战舰可以是一行也可以是一列,而且战舰之间必须隔着狭槽。通过编程算出板上战舰的数量。
除此之外,题目还要求程序只能对板进行一次遍历,使用的额外内存的空间复杂度为O(1),并且不能修改板上战舰和狭槽的分布。
C++程序如下:
class Solution {
public:
int countBattleships(vector<vector<char>>& board) {
int count=0;
for(int i = 0; i < board.size(); i++){
for(int j = 0; j < board[0].size(); j++)
if(board[i][j]=='X'&&(i==0 || board[i-1][j]!='X')&&(j==0 || board[i][j-1] != 'X'))
count++;
}
return count;
}
};函数countBattleships()的返回值为战舰的数量,函数参数为数据类型为二维容器的board,由于需要遍历board,所以设置两层嵌套的循环,外层循环次数为board.size()即板的行数,内层循环次数为board[0].size()即板的列数。
在循环内执行一个判断,由于战舰之间必须隔着狭槽,所以当战舰位于board的最左上角时,战舰的数量加1;当战舰位于第一行时,只要保证战舰左边不挨着另一条战舰即可;当战舰位于第一列时,只要保证战舰上面不挨着另一条战舰即可;当战舰不位于board的四边上,则需要保证战舰的上边和左边不挨着战舰。
根据以上规则,遍历一遍整个board,就可以数出所有符合规则的战舰。
'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.
Could you do it in one-pass, using only
O(1) extra memory and without modifying the value of the board?
题目如上,简化后的题意如下:
给你一个方形的板,上面分布着战舰和狭槽,其中战舰用‘X’表示,狭槽用‘.’表示,但战舰的位置有一定限制。战舰可以是一行也可以是一列,而且战舰之间必须隔着狭槽。通过编程算出板上战舰的数量。
除此之外,题目还要求程序只能对板进行一次遍历,使用的额外内存的空间复杂度为O(1),并且不能修改板上战舰和狭槽的分布。
C++程序如下:
class Solution {
public:
int countBattleships(vector<vector<char>>& board) {
int count=0;
for(int i = 0; i < board.size(); i++){
for(int j = 0; j < board[0].size(); j++)
if(board[i][j]=='X'&&(i==0 || board[i-1][j]!='X')&&(j==0 || board[i][j-1] != 'X'))
count++;
}
return count;
}
};函数countBattleships()的返回值为战舰的数量,函数参数为数据类型为二维容器的board,由于需要遍历board,所以设置两层嵌套的循环,外层循环次数为board.size()即板的行数,内层循环次数为board[0].size()即板的列数。
在循环内执行一个判断,由于战舰之间必须隔着狭槽,所以当战舰位于board的最左上角时,战舰的数量加1;当战舰位于第一行时,只要保证战舰左边不挨着另一条战舰即可;当战舰位于第一列时,只要保证战舰上面不挨着另一条战舰即可;当战舰不位于board的四边上,则需要保证战舰的上边和左边不挨着战舰。
根据以上规则,遍历一遍整个board,就可以数出所有符合规则的战舰。
相关文章推荐
- 题解——Leetcode 419. Battleships in a Board 难度:Medium
- 题解——Leetcode 12. Integer to Roman 难度:Medium
- 题解——Leetcode 16. 3Sum Closest 难度:Medium
- 题解——Leetcode 529. Minesweeper 难度:Medium
- 题解——Leetcode 18. 4Sum 难度:Medium
- LeetCode[127. Word Ladder] 题解 难度[medium]
- 题解——Leetcode 11.Container With Most Water 难度:Medium
- 题解——Leetcode 442. Find All Duplicates in an Array 难度:Medium
- 题解——Leetcode 240. Search a 2D Matrix II 难度:Medium
- 题解——Leetcode 241. Different Ways to Add Parentheses 难度:Medium
- 题解——Leetcode 406. Queue Reconstruction by Height 难度:Medium
- 题解——Leetcode 8. String to Integer (atoi) 难度:Medium
- 题解——Leetcode 5. Longest Palindromic Substring 难度:Medium
- 题解——Leetcode 526. Beautiful Arrangement 难度:Medium
- 题解——Leetcode 2. Add Two Numbers 难度:Medium
- 题解——Leetcode 6. ZigZag Conversion 难度:Medium
- 题解——Leetcode 17.Letter Combinations of a Phone Number 难度:Medium
- LeetCode[377. Combination Sum IV] 难度[medium]
- [置顶] [编程题] LeetCode上的backTracking类型的题目-难度Medium
- LeetCode[126.Word Ladder II]题解 难度[hard]