【发布时间】:2020-12-12 14:18:24
【问题描述】:
当您声明“lastIndexes”数组时,它的大小总是“26”。它不会因输入而改变。那么这是否意味着这个操作是 O(1) 常数空间?请忽略分区数组,我只是对“数组(26)”操作感到好奇。
更新:我不想知道操作的 TIME 复杂度,这将花费多少内存?
var partitionLabels = function(S) {
const partitions = [];
const lastIndexes = new Array(26);
for(var i = 0; i < S.length; i++){
lastIndexes[S.charCodeAt(i) - 97] = i;
}
let start = 0;
while(start < S.length){
let maxIndex = lastIndexes[S.charCodeAt(start) - 97];
let curr = start;
while(curr != maxIndex){
maxIndex = Math.max(maxIndex, lastIndexes[S.charCodeAt(curr++) - 97])
}
partitions.push((curr - start) + 1);
start = curr + 1;
}
return partitions;
};
【问题讨论】:
标签: arrays time-complexity big-o space space-complexity