class Solution {
public:
vector<int> partitionLabels(string s) {
int hash[27] = {0};
// 记录下每个字母出现的最远位置
for(int i = 0; i < s.size(); i++)
{
hash[s[i] - 'a'] = i;
}
int left = 0, right = 0;
vector<int> res;
for(int i = 0 ; i < s.size(); i++)
{
//记录下最右边的边界
right = max(hash[s[i] - 'a'], right);
if(right == i)
{
res.push_back(right - left + 1);
left = i + 1; // 更新左边界
}
}
return res;
}
};
leetcode 763.划分字母区间
最新推荐文章于 2024-07-08 22:12:26 发布