技术栈

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

Ddddddd_1582024-09-23 11:19

题目:

题解:

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;
    }
};
上一篇:idea运行java项目main方法报build failure错误的解决方法
下一篇:C++高精度计时方法总结(测试函数运行时间)
相关推荐
clint456
19 小时前
C++进阶(1)——前景提要
c++
夜悊
1 天前
C++代码示例:进制数简单生成工具
c++
郝学胜_神的一滴
1 天前
CMake 021: IF 条件判据详诠
c++·cmake
_wyt001
2 天前
洛谷 B3930 [GESP202312 五级] 烹饪问题 题解
c++·gesp
玖玥拾
2 天前
C/C++ 数据结构(七)栈、容器适配器
c语言·数据结构·c++·栈·容器适配器
один but you
2 天前
constexpr函数
c++
凡人叶枫
2 天前
Effective C++ 条款41:了解隐式接口和编译期多态
java·开发语言·c++·effective c++
凡人叶枫
2 天前
Effective C++ 条款42:了解 typename 的双重意义
java·linux·服务器·c++
小胖xiaopangss
2 天前
BRpc使用
c++·rpc
-森屿安年-
2 天前
63. 不同路径 II
c++·算法·动态规划
热门推荐
012026年6月AI行业全景:从百模大战到Agent元年,这30天发生了什么?022026 年 AI 编程工具终极横评:Cursor vs Claude Code vs Copilot vs Windsurf032026年6月AI大模型全景报告:GPT-5.6、Claude Opus 4.8、Gemini 3.5,中美AI三足鼎立谁主沉浮?04【AI】2026 年具身智能模型和世界模型总结052026 AI 编程工具终极实战指南:Cursor vs Claude Code vs Copilot,开发者该怎么选?06GitHub 镜像站点07AI科技热点日报 | 2026年6月1日08上线仅72小时被强制下架:Claude Fable 5 的短命09Codex 下载安装指南:Windows 和 macOS 官方版下载10AI一周事件 · 2026-06-03 至 2026-06-09