题目链接
class Solution {
public:
vector
<int> partitionLabels(string S
) {
int n
= S
.size();
vector
<int> pos(26, 0);
for (int i
= 0; i
< n
; ++i
) {
pos
[S
[i
] - 'a'] = i
;
}
vector
<int> ret
;
int pre
= 0;
int t
= pos
[S
[0] - 'a'];
for (int i
= 0; i
< n
; ++i
) {
if (t
< i
) {
ret
.push_back(i
- pre
);
pre
= i
;
}
t
= max(t
, pos
[S
[i
] - 'a']);
}
ret
.push_back(n
- pre
);
return ret
;
}
};
转载请注明原文地址: https://lol.8miu.com/read-29640.html