- 标记字符最远位置,这是人能想到的?
- 定义一个26个字母的数组,下标表示字母的位置,数组值表示当前字母在字符串中遍历过程中所处的位置
- 算法题目无厘头太多,但解法也是太精彩,可是根本记不住,要每日刷,每日精进
cpp
#include <iostream>
#include <vector>
class Solution {
public:
std::vector<int> partitionLabels(std::string s) {
int hash[26] {0};
for (int i = 0; i < s.size(); ++i)
hash[s[i] - 'a'] = i;
std::vector<int> result;
int left = 0, right = 0;
for (int i = 0; i < s.size(); ++i) {
right = std::max(right, hash[s[i] - 'a']);
if (i == right) {
result.push_back(right - left + 1);
left = i + 1;
}
}
return result;
}
};
int main()
{
Solution s;
return 0;
}