BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / acm-icpc / #97268同步于 2018/11/29
该镜像源已超过 30 天没有更新,可能在源站已被删除。
ACM_ICPC机器人发帖

关于leetcode 830. Positions of Large Groups 的一点疑问。

niuchuang123
2018/11/29镜像同步4 回复
题目:leetcode830. Positions of Large Groups 题目链接: 题目的大意是: 代码提交结果:class Solution { public List<List<Integer>> largeGroupPositions(String S) { List<List<Integer>> list = new LinkedList<>(); char[] nums = S.toCharArray(); int count = 1; for(int i = 0; i < nums.length - 1; i++){ if(count < 3){ if(nums[i] == nums[i + 1]){ count++; } else{ count = 1; } }else if(count >= 3){ if(nums[i] == nums[i + 1]){ count++; }else{ List<Integer> lists = new LinkedList<>(); lists.add(i - count + 1, i); // IndexOutOfBoundsException 为什么会这样呢? list.add(lists); count = 1; } } } return list; } } 我对这道题有如下想法: 具体代码见附件:
订阅后,新回复会通过你的通知中心匿名送达。
4 条回复
niuchuang123机器人#1 · 2018/11/29
我哪里出错了呢,请大佬指出来,索引越界异常的问题,但是我看不出来错在哪。哭泣
l947069962机器人#2 · 2018/11/30
lz数据结构基础很弱啊 list.add(int index, E element) 是在把element放到list的index位置, index在list的下标中越界的话,就会抛这个异常 看下这个Solution的写法吧 https://leetcode.com/problems/positions-of-large-groups/solution/ 加油!!!
niuchuang123机器人#3 · 2018/11/30
【 在 l947069962 的大作中提到: 】 : lz数据结构基础很弱啊 : list.add(int index, E element) 是在把element放到list的index位置, index在list的下标中越界的话,就会抛这个异常 : 看下这个Solution的写法吧 https://leetcode.com/problems/positions-of-large-groups/solution/ : ................... 确实基础弱,被你发现了呢,谢谢。
zuozhonghu机器人#4 · 2018/12/3
【 在 niuchuang123 的大作中提到: 】 : 题目:leetcode830. Positions of Large Groups : 题目链接: : 题目的大意是: : ................... class Solution { public: vector<vector<int>> largeGroupPositions(string S) { vector<vector<int>>res; if(S.empty()) return res; int l = 0; int r = 0; while(r < S.size()){ if(S[l] == S[r]){ r ++; }else{ if(r - l > 2){ res.push_back(vector<int>({l, r - 1})); } l = r; } } if(r - l > 2){ res.push_back(vector<int>({l, r - 1})); } return res; } };