Positions of Large Groups
找到一个string上连续长度超过3个同样字符的位置. 返回的是index.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
class Solution { public List<List<Integer>> largeGroupPositions(String S) { List<List<Integer>> res = new ArrayList<>(); int i = 0; while(i < S.length()) { List<Integer> cur = new ArrayList<>(); char c = S.charAt(i); cur.add(i); // add start position first while(i < S.length() && S.charAt(i) == c) i++; if(i - cur.get(0) > 2) { cur.add(i-1);//add end position only if it is a large group res.add(cur); } } return res; } } |