#include <map>
using namespace std;
class Solution {
public:
vector<int> partitionLabels(string S) {
vector<int> ans;
map<int, int> m;
for (int i = 0; i < S.size(); i++) {
m[S[i]] = i;
}
/*for (auto item : m) {
cout << item.first << "---" << item.second << endl;
}*/
int start = 0;
int end = 0;
for (int i = 0; i < S.size();i++) {
// 第一次出现任意一个字母的 就把最终的区间给订好了
end = max(end, m[S[i]]);
if (end == i) {
ans.push_back(end - start + 1);
// update start
start = end + 1;
}
}
return ans;
}
};
leetcode763 划分字母区间 方法2
最新推荐文章于 2023-02-24 09:40:44 发布