文章目录
我的天免费的4o太好用了
我的天免费的4o太好用了
我的天免费的4o太好用了
重要的事情说三遍
题目详情
- 甲板上的战舰
给你一个大小为 m x n 的矩阵 board 表示甲板,其中,每个单元格可以是一艘战舰 'X' 或者是一个空位 '.' ,返回在甲板 board 上放置的 战舰 的数量。
战舰 只能水平或者垂直放置在 board 上。换句话说,战舰只能按 1 x k(1 行,k 列)或 k x 1(k 行,1 列)的形状建造,其中 k 可以是任意大小。两艘战舰之间至少有一个水平或垂直的空位分隔 (即没有相邻的战舰)。
示例 1:
输入:board = [["X",".",".","X"],[".",".",".","X"],[".",".",".","X"]]
输出:2
示例 2:
输入:board = [["."]]
输出:0
提示:
m == board.length
n == board[i].length
1 <= m, n <= 200
board[i][j] 是 '.' 或 'X'
进阶:你可以实现一次扫描算法,并只使用 O(1) 额外空间,并且不修改 board 的值来解决这个问题吗?
思路:
我先避免考虑边界问题,为什么呢,因为我想到了前天的每日一题,而且这样比较好做
我就申请了个数组来避免边界问题
然后我就想如果是X我就ans++;如果不是我就不管他,然后我搞个判断的二维数组。看看如果是算上数的我就设这个格格为1不是就是0;
然后遍历board,如果是X,先给他算上数ans++,要是他上下左右还有的话就不算了再把它减了,这里上下左右就错了。然后先加上再减去这种方法可能还有更容易的,我没看题解欢迎评论区讨论。
关键:chatGPT配合纠正错误思路
我不是想上下左右都要判断吗,然后下面的示例就错了
[["X",".","X","X","X","X","X","X","X","X"],
[".","X",".",".",".",".",".",".",".","."],
[".",".",".",".",".","X",".","X",".","X"],
["X","X","X","X","X",".","X",".",".","."],
[".",".",".",".",".","X",".",".","X","X"],
["X",".",".","X",".",".",".",".",".","."],
[".",".","X",".","X",".","X","X","X","X"]
,["X",".","X",".","X",".",".",".",".","."]
,["X",".","X",".","X",".",".","X",".","."],
[".","X",".",".","X",".",".","X",".","X"]]
你这样看,很乱。然后我就想让chatGPT给我整理一下这个实例。让我具体看看是哪错了然后他直接给我个图还是正确的,我直接一个大震惊
然后我觉得我还是对的,他让我判断上左不用下右,我这一想,对哦,不需要考虑下右因为下右还没有初始化,
然后我按他的改后,还是错的,
最后在CHATGPT的指导下,发现我的数组的初始化错了,我不是把数组整体向右下角移了一个,这里应该初始化为·,而我没管他。这里靠CHATGPT正确的找到了错误,大大的节省了时间。
之前感觉他还挺鸡肋的,不过在4o后智能了很多很多很多,并且可以生成图了,而且有免费的次数,无敌了很多
感觉如果是3.5的话,只会给正确答案,而不会按照你的思路,更改你的复杂的思路中的错误最后附上正确代码
正确代码:
cpp
class Solution {
public:
int countBattleships(vector<vector<char>>& board) {
int ans = 0;
int n = board.size();
int m = board[0].size();
vector<vector<char>> boar(n+2, vector<char>(m+2, '.'));
vector<vector<int>> a(n+2, vector<int>(m+2, 0));
for(int i=1; i<=n; i++) {
for(int j=1; j<=m; j++) {
boar[i][j] = board[i-1][j-1];
}
}
for(int i=1; i<=n; i++) {
for(int j=1; j<=m; j++) {
if(boar[i][j] == 'X') {
a[i][j] = 1;
ans++;
if(a[i-1][j] == 1 || a[i][j-1] == 1||a[i+1][j] == 1 || a[i][j+1] == 1) {
ans--;
}
}
}
}
return ans;
}
};
至于进阶我还没有做,太晚了,大家晚安,那里也有很多好的题解,我就是表达一下GPT帮我找到错的兴奋
qwq