LeetCode 题解之 Positions of Large Groups

1、题目描述

2、问题分析

从头遍历字符串,使用一个局部迭代器和局部变量记录该字符个数。如果个数>= 3 ,则将此时的迭代器位置和局部迭代器的位置保存到局部vector中。再将这个局部vector 保存到 最终的结果vector中。

3、代码

 1 vector<vector<int>> largeGroupPositions(string S) {
 2         vector<vector<int>> r ;
 3         string::iterator it = S.begin() ;
 4         
 5         while( it != S.end() ){
 6             int n = 0;
 7             string::iterator it_a = it;
 8             vector<int> e;
 9             while( *it_a == *it && it_a != S.end() ){
10                 n++;
11                 ++it_a;
12             }
13             
14             if( n >= 3 ){
15                 e.push_back(it-S.begin() );
16                 e.push_back(it_a - S.begin() - 1);
17                 r.push_back( e );
18             }
19             it = it_a;
20         }
21         return r;  
22     }

猜你喜欢

转载自www.cnblogs.com/wangxiaoyong/p/9300472.html