Leetcode - 830 Positions of Large Groups (Easy)
题目描述:给定一个字符串由连续相同的字母组成,找出连续字母数量超过 3 的起始位置。
Input: "abbxxxxzzy"
Output: [[3,6]]
Explanation: "xxxx" is the single large group with starting 3 and ending positions 6.
public List<List<Integer>> largeGroupPositions(String S) {
List<List<Integer>> res = new ArrayList<>();
int i = 0;
for (int j = 0; j < S.length(); j++) {
if (j == S.length() - 1 || S.charAt(j) != S.charAt(j + 1)) {
if (j - i + 1 >= 3) {
res.add(Arrays.asList(new Integer[]{i, j}));
}
i = j + 1;
}
}
return res;
}