class Solution {
public:
vector<int> partitionLabels(string s) {
vector<int> res;
unordered_map<char, int> last;//哈希表,存放字母最后出现的位置
for (int i = 0; i < s.size(); i++)
{
last[s[i]] = i;
}
int start = 0, end = 0;
for (int i = 0; i < s.size(); i++)
{
end = max(end, last[s[i]]);
if (i == end)//当当前的点正好等于last区间右端点时,将其作为结果推进结果数组中
{
res.push_back(end - start + 1);
start = i + 1;
}
}
return res;
}
};
03-15
70
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-03
146
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-20
08-20
08-20