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;
    }
};
相关推荐
追烽少年x26 分钟前
STL中的设计模式(一)
c++·设计模式
Lumbrologist29 分钟前
【C++】零基础入门 · 第 3 节:条件判断(if、switch)
开发语言·c++·算法
LeocenaY42 分钟前
C/C++ 面试题总结
java·c++·面试
xier_ran1 小时前
【infra之路】从“三堵叹息之墙”到异构计算的狂飙
开发语言·c++·算法
curry____3031 小时前
邻接矩阵 和 领接表 和 链式前向星对比
数据结构·c++·算法
是星辰吖~1 小时前
C++_string类_调用及模拟实现
开发语言·c++
csdn_aspnet1 小时前
C++ 算法 LeetCode 编号 70 - 爬楼梯
开发语言·c++·算法·leetcode
圣保罗的大教堂2 小时前
leetcode 2770. 达到末尾下标所需的最大跳跃次数 中等
leetcode
I Promise342 小时前
C++ 单例模式超详细讲解
开发语言·c++·单例模式
Emerson_20262 小时前
stack,queue,list的区别和联系
数据结构·c++·list·queue·stack