LeetCode报错

报错原因:
Line 923: Char 9: runtime error: reference binding to null pointer of type ‘std::__cxx11::basic_string<char, std::char_traits, std::allocator >’ (stl_vector.h)

程序中具体原因:
    vector数组为空的时候,会出现这个错误。在程序中就是for语句出错,因为当数组为空时,len=0,len-1=-1,所以在for循环中会出错

出错具体代码

class Solution {public:string longestCommonPrefix(vector<string>& strs) {int len=strs.size();string result="";int i=0;while(strs[0][i]!='\0'){for(int j=0;j<len-1;++j){if(strs[j][i]!=strs[j+1][i])return result;}result+=strs[0][i];++i;}return result;}
};

改正后代码(判断数组为空,提前return)

class Solution {public:string longestCommonPrefix(vector<string>& strs) {int len=strs.size();string result="";if(len==0) return result;// 判断是否为空int i=0;while(strs[0][i]!='\0'){for(int j=0;j<len-1;++j){if(strs[j][i]!=strs[j+1][i])return result;}result+=strs[0][i];++i;}return result;}
};

LeetCode报错:Line 923: Char 9: runtime error: reference binding to null pointer of type ‘std::__cxx11:相关推荐

  1. Line 923: Char 9: runtime error: reference binding to null pointer of type ‘int‘ (stl_vector.h)

    Leetcode 报错 Line 923: Char 9: runtime error: reference binding to null pointer of type 'int' (stl_ve ...

  2. leetcode报错runtime error: reference binding to null pointer of type ‘std::vector<std::__cxx11::basic_

    leetcode报错:runtime error: reference binding to null pointer of type 'std::vector<std::__cxx11::ba ...

  3. 力扣报错:runtime error: reference binding to null pointer of type ‘std::vector<int

    出错原因: 有for循环或者while时,没有考虑数组为空的情况,且将判断数组为空条件写在比较靠下的位置了,比如下面时错误的: 参考文章: https://blog.csdn.net/qq_36421 ...

  4. LeetCode Line x: Char x: runtime error: member access within null pointer of type ‘struct ListNode

    今天做leetcode的时候使用链表遇到了这个问题,百思不得其解.遂疯狂百度标题,发现大伙都说了一个问题,那就是没有对指针判空.没办法,鄙人能力有限不知道为什么这个环境下不判空会报错,不过既然知道原因 ...

  5. runtime error: reference binding to null pointer of type ‘int‘ (stl_vector.h)

    报错原因:没有指定数组大小 vector在还没有分配任何空间时还不能像数组一样用下标形式去访问vector的(v[0]也不行)!!!否则编译通过但报运行错误runtime error! vector是 ...

  6. runtime error: reference binding to null pointer of type ‘int‘ 问题

    这个问题我在做LeetCode的股票最大利润问题时运行遇到的,此问题为引用绑定了int类型的空指针. 根据个人遇到的情况的总结,出现这种问题的原因一般有二: 1.越界访问,查看在用下标访问vector ...

  7. runtime error: member access within null pointer of type ‘MyLinkedList::ListNode‘ (solution.cpp)

    ERROR情况: Line 40: Char 12: runtime error: member access within null pointer of type 'MyLinkedList::L ...

  8. 【Leetcode记录】runtime error: member access within null pointer of type ‘ListNode‘ (solution.cpp) SUMMA

    环形链表快慢指针: runtime error: member access within null pointer of type 'ListNode' (solution.cpp) SUMMARY ...

  9. runtime error: member access within null pointer of type ‘struct ListNode‘错误

    LeetCode203错误分析: (3条消息) LeetCode:runtime error: member access within null pointer of type 'struct Li ...

最新文章

  1. 找出重复的那个数字的异或算法
  2. Sql Server 2005 分页
  3. python算法的基本原理_kNN算法基本原理与Python代码实践
  4. php判断pdf页码,PHP_PHP简单读取PDF页数的实现方法,本文实例讲述了PHP简单读取PDF - phpStudy...
  5. mysql慢查询分析工具和分析方法
  6. zoj3802:easy 2048 again(状压dp)
  7. Ext.example.msg()应用
  8. 【转】几个颇有创意的网站推广方法
  9. 亚马逊运营教程,三招学会亚马逊
  10. 科技进化的终点,与荣耀全场景的起点
  11. 如何打造高端的数据报表?
  12. HTML,CSS 三角形制作案例(等边三角形 直角三角形)
  13. LODOP属性和方法
  14. 【Linux网络安全】Linux操作系统安全配置(超全超详细)
  15. 弘辽科技:淘宝14天降权会恢复吗?多久能恢复正常?
  16. Java实现建造者模式
  17. MQTT协议详解 二、MQTT控制包格式
  18. 中文版3ds Max 2012完全自学教程pdf
  19. 微信小程序使用wxparse,显示图片的相对路径问题
  20. 计算机专业论文要交源代码吗,计算机毕业论文源代码

热门文章

  1. 35个强大的UI设计教程
  2. Java实现第八届蓝桥杯拉马车
  3. 川崎机器人几百个示教点位置的动态修正
  4. Win8安装程序出现2502、2503错误解决方法
  5. 10倍杠杆炒股是什么意思
  6. 常用前端技术有哪些?
  7. 神经网络学习小记录58——Keras GhostNet模型的复现详解
  8. 设计模式之结构型模型
  9. 诺思格医药通过注册:年营收6亿 实控人武杰为美国籍
  10. Error:(63, 20) Failed to resolve: com.github.chrisbanes:PhotoView:2.0.0