我好菜
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| class Solution { public: vector<int> partitionLabels(string S) { int c[26]; vector<int> ret; for(int i = 0; i < S.size(); ++i) { c[S[i] - 'a'] = i; } int anchor = 0; int last = 0; for(int i = 0; i < S.size(); ++i) { last = max(last, c[S[i] - 'a']); if(i == last) { ret.push_back(last + 1 - anchor); anchor = last + 1; } } return ret; } };
|
每走到一步就判断他的最后一位,差不多就寻求一个最大区间