C++ | Leetcode C++题解之第419题棋盘上的战舰

题目:

题解:

cpp 复制代码
class Solution {
public:
    int countBattleships(vector<vector<char>>& board) {
        int row = board.size();
        int col = board[0].size();
        int ans = 0;
        for (int i = 0; i < row; ++i) {
            for (int j = 0; j < col; ++j) { 
                if (board[i][j] == 'X') {
                    if (i > 0 && board[i - 1][j] == 'X') {
                        continue;
                    }
                    if (j > 0 && board[i][j - 1] == 'X') {
                        continue;
                    }
                    ans++;
                }
            }
        }
        return ans;
    }
};
相关推荐
追随者永远是胜利者3 小时前
(LeetCode-Hot100)4. 寻找两个正序数组的中位数
java·算法·leetcode·职场和发展·go
追随者永远是胜利者3 小时前
(LeetCode-Hot100)2. 两数相加
java·算法·leetcode·go
L_Aria4 小时前
3824. 【NOIP2014模拟9.9】渴
c++·算法·图论
ShineWinsu4 小时前
对于模拟实现C++list类的详细解析—上
开发语言·数据结构·c++·算法·面试·stl·list
Mr YiRan4 小时前
C++语言类中各个重要函数原理
java·开发语言·c++
程序员酥皮蛋4 小时前
hot 100 第二十九题 29.删除链表的倒数第 N 个结点
数据结构·算法·leetcode·链表
stripe-python4 小时前
十二重铲雪法(下)
c++·算法
踩坑记录5 小时前
leetcode hot100 994. 腐烂的橘子 medium bfs
leetcode·宽度优先
D_evil__5 小时前
【Effective Modern C++】第五章:右值引用、移动语义和完美转发:29. 认识移动操作的缺点
c++
化学在逃硬闯CS5 小时前
【Leetcode热题100】108.将有序数组转换为二叉搜索树
数据结构·c++·算法·leetcode