赞
踩
- class Solution {
- public:
- vector<int> partitionLabels(string s) {
- vector<int> last(26,0);
- int n=s.size();
- for(int i=0;i<n;i++){
- last[s[i]-'a']=i;
- }
- vector<int> ans;
- int start=0,end=0;
- int Max=0;
- while(end<n){
- Max=max(Max,last[s[end]-'a']);
- if(end==Max){
- ans.push_back(end-start+1);
- start=end+1;
- end=start;
- }else{
-
- end++;
- }
- }
- return ans;
- }
- };
- class Solution {
- public:
- vector<int> partitionLabels(string s) {
- vector<int> last(26,0);
- int n=s.size();
- for(int i=0;i<n;i++){
- last[s[i]-'a']=i;
- }
- vector<int> ans;
- int start=0,end=0;
- int Max=0;
- for(int i=0;i<n;i++){
- Max=max(Max,last[s[i]-'a']);
- if(Max==i){
- ans.push_back(i-start+1);
- start=i+1;
- }
- }
- return ans;
- }
- };
记录每一个单词的最后位置(last数组)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。