麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

LeetCode題解:Battleships in a Board

2019-11-14 10:34:03
字體:
來源:轉載
供稿:網友

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...XIn the above board there are 2 battleships.

Invalid Example:

...XXXXX...X

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

思路:

簡單的思路是做搜索。但是考慮到每個船和每個船之間至少橫向縱向有一個空格,那么如果只考慮每艘船的右下角:

XX.X...X

這樣的話,右下角的X右邊和下面都是空格,每次碰到符合這個條件的X就認為碰到一個船,否則不管。

題解:

int countBattleships(const std::vector<std::vector<char>>& board) {    const int M = board.size();    const int N = board[0].size();    int numShips(0);    for(int i = 0; i < M; ++i) {        for(int j = 0; j < N; ++j) {            if (board[i][j] == 'X') {                numShips += ((i < M - 1 && board[i + 1][j] == '.') || (i == M - 1)) &&                            ((j < N - 1 && board[i][j + 1] == '.') || (j == N - 1));            }        }    }    return numShips;}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 黄www片| 日韩精品久久久久久久九岛 | 国产精品久久久久久久亚洲按摩 | 亚州综合网 | 欧美综合成人 | 九九综合视频 | 欧美成人精品欧美一级乱黄 | 精品国产一区二区三区四区阿崩 | 国产精品99久久久久久久vr | 国产91精品久久久久久 | 嗯~啊~弄嗯~啊h高潮视频 | 欧美日韩专区国产精品 | 欧美一级网 | 日韩视频观看 | 美女黄污视频 | 成人毛片100部免费观看 | 中国hdxxxx护士爽在线观看 | 精品国产一区二区三区免费 | 欧美成人精品一区 | 成人视屏在线 | 亚洲aⅴ免费在线观看 | 麻豆视频在线观看免费网站 | 日韩精品二区 | 国产亚洲黑人性受xxxx精品 | 国产一级毛片高清视频完整版 | 欧美福利视频一区二区三区 | 午夜视频亚洲 | 一区二区三区日韩在线观看 | 久色亚洲| china对白普通话xxxx | 国产精品成人亚洲一区二区 | 污片视频在线观看 | 国产成人综合在线观看 | av日韩在线免费观看 | 久草在线公开视频 | 神马久久精品综合 | 国产一级αv片免费观看 | 色播视频在线播放 | 国产电影精品久久 | 高清av免费 | 中国洗澡偷拍在线播放 |