830. Positions of Large Groups
问题:
给定字符串,返回连续相同字符子串长度>=3的始终位置集合。
Example 1: Input: "abbxxxxzzy" Output: [[3,6]] Explanation: "xxxx" is the single large group with starting 3 and ending positions 6. Example 2: Input: "abc" Output: [] Explanation: We have "a","b" and "c" but no large group. Example 3: Input: "abcdddeeeeaabbbcd" Output: [[3,5],[6,9],[12,14]]
解法:
参考代码:
1 class Solution { 2 public: 3 vector<vector<int>> largeGroupPositions(string S) { 4 vector<vector<int>> res; 5 int count=0; 6 int i; 7 char curc; 8 for(i=0; i<S.length(); i++){ 9 if(curc!=S[i]) { 10 if(count>=3){ 11 res.push_back({i-count, i-1}); 12 } 13 curc = S[i]; 14 count=0; 15 } 16 count++; 17 } 18 if(count>=3){ 19 res.push_back({i-count, i-1}); 20 } 21 return res; 22 } 23 };