【发布时间】:2018-10-16 05:38:42
【问题描述】:
class Solution {
public List<List<Integer>> largeGroupPositions(String S) {
//int k=0;
List<Integer> l1 = new ArrayList<Integer>();
List<List<Integer>> l2 = new ArrayList<List<Integer>>();
int n = S.length();
int count =1, i1=0, i2=0;
for(int i=1; i<n; i++){
if(S.charAt(i)==S.charAt(i-1)){
count++;
}else{
i2 = i-1;
if(count>=3){
l1.add(i1);
l1.add(i2);
l2.add(l1);
}
count =1;
i1=i;
}
}
return l2;
}
}
我需要这个输出[[3,5],[6,9],[12,14]],但我得到[[3,5,6,9,12,14],[3,5,6,9,12,14],[3,5,6,9,12,14]],如果我在其他部分使用 l1.clear(),那么 l2 也会发生变化
【问题讨论】:
-
你调试了吗?
-
问题是你在重用
l1。将l1的声明移到if(count>=3)条件内。 -
阅读ericlippert.com/2014/03/05/how-to-debug-small-programs,了解如何调试问题的一些技巧。