a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

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

LeetCode題解:Battleships in a Board

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

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;}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: bxbx成人精品一区二区三区 | 国产成人精品白浆久久69 | 国产精品91av | 日本免费三片免费观看 | 日韩一二三区在线观看 | 亚洲欧美综合一区 | 黄色成人av | 欧美成人a交片免费看 | 亚洲一区欧美 | 日韩久久综合 | 51ⅴ精品国产91久久久久久 | 特黄网站 | 九九热在线免费视频 | 国产一区亚洲 | 欧美亚洲在线 | 91激情在线 | 国产精品国产 | 色欧美视频 | 天天插天天操天天干 | 亚洲 欧美 精品 | 欧美精品二区三区四区免费看视频 | 久久久久久一区二区 | 成人一区二区三区 | 国产欧美第一页 | 91久久久久久| 亚洲午夜性视频 | 一区二区三区视频免费在线观看 | 99久久免费观看 | 久久亚洲国产精品日日av夜夜 | 亚洲成人第一页 | 影音先锋在线看片资源 | 久久久久久电影 | 久久久久a | 国产一级黄片毛片 | 欧美色性 | 欧美激情小视频 | www日| 成人免费毛片高清视频 | 国产视频一区在线 | 日韩视频在线观看视频 | 国产美女在线精品免费观看网址 |