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;
    }
};
相关推荐
橘颂TA1 分钟前
【剑斩OFFER】算法的暴力美学——leetCode 103 题:二叉树的锯齿形层序遍历
算法·leetcode·结构与算法
客卿1234 分钟前
C语言实现数组串联--力扣冒险
c语言·开发语言·leetcode
我就想睡到自然醒8 分钟前
【C++基础STL1】数组和vector
c++
玖釉-10 分钟前
[Vulkan 学习之路] 04 - 选妃环节:挑选物理设备与队列族
c++·windows·图形渲染
三万棵雪松12 分钟前
【AI小智硬件程序(九)】
c++·人工智能·嵌入式·esp32·ai小智
Lips61116 分钟前
2026.1.13力扣刷题笔记
笔记·算法·leetcode
老鼠只爱大米30 分钟前
LeetCode算法题详解 76:最小覆盖子串
算法·leetcode·双指针·滑动窗口·最小覆盖子串·minwindow
HABuo31 分钟前
【linux进程控制(一)】进程创建&退出-->fork&退出码详谈
linux·运维·服务器·c语言·c++·ubuntu·centos
想唱rap44 分钟前
MySQL内置函数
linux·运维·服务器·数据库·c++·mysql
玖釉-1 小时前
[Vulkan 学习之路] 10 - 掌握 SPIR-V:编写你的第一个着色器 (Shader Modules)
c++·windows·图形渲染