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;
    }
};
相关推荐
noipp19 分钟前
推荐题目:洛谷 P6231 [JSOI2013] 公交系统
c语言·数据结构·c++·算法·游戏·洛谷·luogu
c2385633 分钟前
C/C++每日一练6
c语言·c++·算法
念恒123061 小时前
网络基础
linux·网络·c++
mct1231 小时前
c++ iconv 字符utf-8转换gb2312失败
开发语言·c++
ziguo11221 小时前
Windows API 文件操作超级指南——从入门到内核
c++·windows·visualstudio
alphaTao3 小时前
LeetCode 每日一题 2026/7/20-2026/7/26
算法·leetcode
zhangfeng11334 小时前
Ascendc 大语言模型框架 AscendCraft 和pypto 的区别 pypto生成算子c++源码吗
c++·人工智能·语言模型·算子开发
一只小灿灿4 小时前
C++ 修饰符全面详解
开发语言·c++
圣保罗的大教堂4 小时前
leetcode 3514. 不同 XOR 三元组的数目 II 中等
leetcode
code_pgf4 小时前
C/C++ 中 `typedef` 关键字详解
c语言·c++