题解 | #分品种#
分品种
https://www.nowcoder.com/practice/9af4e93b04484df79d4cc7a863343b0b
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param s string字符串
* @return int整型vector
*/
vector<int> partitionLabels(string s) {
// write code here
map<char, int> mp;
unordered_set<char> char_set;
vector<int> ans;
for (auto c : s) {
mp[c]++;
}
int len = 0;
for (auto c : s) {
mp[c]--;
len++;
char_set.insert(c);
if (mp[c] == 0) {
char_set.erase(c);
}
if (char_set.size() == 0) {
ans.push_back(len);
len = 0;
}
}
return ans;
}
};
