c++ LeetCode问题:激光束的数量在银行[关闭]

qojgxg4l  于 2024-01-09  发布在  其他
关注(0)|答案(1)|浏览(142)

**已关闭。**此问题需要debugging details。目前不接受回答。

编辑问题以包括desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答问题。
3天前关闭。
Improve this question
我在解给定的Leetcode Problem
我已经为这个问题写了代码。代码可以在给定的测试用例中工作,但是当我试图提交代码时,它给出了一个运行时错误,如下所示:

  1. Line 1037: Char 9: runtime error: reference binding to null pointer of type 'int' (stl_vector.h)
  2. SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/stl_vector.h:1046:9

字符串
对于给定代码:

  1. class Solution {
  2. public:
  3. int numberOfBeams(vector<string>& bank) {
  4. int TotalBeam=0;
  5. vector<int> occurOne;
  6. for(int i=0;i<bank.size();i++)
  7. {
  8. int numOne=0;
  9. for(int j=0;j<bank[i].length();j++)
  10. {
  11. if(bank[i][j]=='1')
  12. numOne++;
  13. }
  14. if(numOne!=0)
  15. occurOne.push_back(numOne);
  16. }
  17. for(int i=0;i<occurOne.size()-1;i++)
  18. {
  19. TotalBeam+=occurOne[i]*occurOne[i+1];
  20. }
  21. return TotalBeam;
  22. }
  23. };


我以为我的代码有问题。然而,它在我的本地IDE,OpenMiz在线C++编译器和onlinegdb编译器上运行良好。所以,我不明白真正的问题是什么?

qacovj5a

qacovj5a1#

只需添加一个条件if(occurOne.size()< 2){ return 0 }。然后运行时问题就解决了。

相关问题