遍历一遍,统计相同的次数并且更新。
class Solution {
public List<List<Integer>> largeGroupPositions(String S) {
List<List<Integer>> res = new ArrayList<>();
char[] s = S.toCharArray();
int count = 1;
for(int i=0;i<s.length-1;i++){
if(s[i]==s[i+1]){
count++;
}else{
if(count>=3){
List<Integer> tmp = new ArrayList<>();
tmp.add(i+1-count);
tmp.add(i);
res.add(tmp);
}
count = 1;
}
}
if(count>=3){
List<Integer> tmp = new ArrayList<>();
tmp.add(s.length-count);
tmp.add(s.length-1);
res.add(tmp);
}
return res;
}
}
竟然击败100%,嘻嘻嘻~