题解 | #分品种#
分品种
https://www.nowcoder.com/practice/9af4e93b04484df79d4cc7a863343b0b
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param s string字符串
* @return int整型vector
*/
vector<int> partitionLabels(string s) {
// write code here
vector<int> ret;
unordered_set<char> se;
for (int i = 0; i < s.size(); ++i)
{
int count = 1;
for (int j = i + 1; j < s.size(); ++j)
{
if (se.find(s[j]) != se.end() || s[j] == s[i])
{
int tmp = i;
while (tmp <= j)
se.insert(s[tmp++]);
count += j - i;
i = j;
}
}
ret.push_back(count);
}
return ret;
}
};
查看8道真题和解析
