**已关闭。**此问题需要debugging details。目前不接受回答。
编辑问题以包括desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答问题。
3天前关闭。
Improve this question的
我在解给定的Leetcode Problem
我已经为这个问题写了代码。代码可以在给定的测试用例中工作,但是当我试图提交代码时,它给出了一个运行时错误,如下所示:
Line 1037: Char 9: runtime error: reference binding to null pointer of type 'int' (stl_vector.h)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/stl_vector.h:1046:9
字符串
对于给定代码:
class Solution {
public:
int numberOfBeams(vector<string>& bank) {
int TotalBeam=0;
vector<int> occurOne;
for(int i=0;i<bank.size();i++)
{
int numOne=0;
for(int j=0;j<bank[i].length();j++)
{
if(bank[i][j]=='1')
numOne++;
}
if(numOne!=0)
occurOne.push_back(numOne);
}
for(int i=0;i<occurOne.size()-1;i++)
{
TotalBeam+=occurOne[i]*occurOne[i+1];
}
return TotalBeam;
}
};
型
我以为我的代码有问题。然而,它在我的本地IDE,OpenMiz在线C++编译器和onlinegdb编译器上运行良好。所以,我不明白真正的问题是什么?
1条答案
按热度按时间qacovj5a1#
只需添加一个条件if(occurOne.size()< 2){ return 0 }。然后运行时问题就解决了。