题目地址:链接
思路:
- 使用map记录最远字符串的地址
- 重新遍历,如果当前内容等于最远字符串地址,那么入队列
function partitionLabels(s: string): number[] {
let ans = [];
const n = s.length;
const map = new Map();
for(let i = 0; i < n; i ++) {
map.set(s[i], i);
}
let maxIdx = 0;
for(let i = 0; i < n; i ++) {
let q = map.get(s[i]);
maxIdx = Math.max(maxIdx, q);
if(maxIdx <= i) {
ans.push(i);
maxIdx ++;
}
}
ans = ans.map((num, idx) => {
if(idx) return num - ans[idx - 1];
else return num + 1
})
return ans;
};
8万+

被折叠的 条评论
为什么被折叠?



