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;
    }
};
相关推荐
ShineWinsu8 小时前
对于C++:IO流状态的详细解析
c++·面试·io·cin·io流状态·goodbit·failbit
Lzg_na9 小时前
constexpr的优势
c++
想吃火锅100510 小时前
【leetcode】200. 岛屿数量
算法·leetcode·职场和发展
王维同学10 小时前
进程模块枚举、映像身份与线程启动地址关联
c++·windows·安全
hold?fish:palm11 小时前
24 回文链表
数据结构·c++·链表
举手11 小时前
Dispatcher模块剖析
linux·c++
Nil20811 小时前
leetcode 54螺旋矩阵
算法·leetcode·矩阵
依然鸣12 小时前
PTA团体程序设计天梯赛L1真题讲解L1-077-080
开发语言·c++·算法·深度优先·pat考试·图论
库玛西13 小时前
现代 C++ 智能指针全景指南:从 RAII 思想到工业级实践
c语言·开发语言·c++·笔记·面试
liulilittle14 小时前
无锁并发容器的设计与实现原理
开发语言·c++·set·map·并发·无锁·lock-free