comments | difficulty | edit_url | tags | ||||
---|---|---|---|---|---|---|---|
true |
中等 |
|
我们可以使用游程编码(即 RLE )来编码一个整数序列。在偶数长度 encoding
( 从 0 开始 )的游程编码数组中,对于所有偶数 i
,encoding[i]
告诉我们非负整数 encoding[i + 1]
在序列中重复的次数。
- 例如,序列
arr = [8,8,8,5,5]
可以被编码为encoding =[3,8,2,5]
。encoding =[3,8,0,9,2,5]
和encoding =[2,8,1,8,2,5]
也是arr
有效的 RLE 。
给定一个游程长度的编码数组,设计一个迭代器来遍历它。
实现 RLEIterator
类:
RLEIterator(int[] encoded)
用编码后的数组初始化对象。int next(int n)
以这种方式耗尽后n
个元素并返回最后一个耗尽的元素。如果没有剩余的元素要耗尽,则返回-1
。
示例 1:
输入: ["RLEIterator","next","next","next","next"] [[[3,8,0,9,2,5]],[2],[1],[1],[2]] 输出: [null,8,8,5,-1] 解释: RLEIterator rLEIterator = new RLEIterator([3, 8, 0, 9, 2, 5]); // 这映射到序列 [8,8,8,5,5]。 rLEIterator.next(2); // 耗去序列的 2 个项,返回 8。现在剩下的序列是 [8, 5, 5]。 rLEIterator.next(1); // 耗去序列的 1 个项,返回 8。现在剩下的序列是 [5, 5]。 rLEIterator.next(1); // 耗去序列的 1 个项,返回 5。现在剩下的序列是 [5]。 rLEIterator.next(2); // 耗去序列的 2 个项,返回 -1。 这是由于第一个被耗去的项是 5, 但第二个项并不存在。由于最后一个要耗去的项不存在,我们返回 -1。
提示:
2 <= encoding.length <= 1000
encoding.length
为偶0 <= encoding[i] <= 109
1 <= n <= 109
- 每个测试用例调用
next
不高于1000
次
我们定义两个指针
每次调用 next(n)
时,我们判断当前游程编码中剩余的字符数
若
时间复杂度 next(n)
的次数。
class RLEIterator:
def __init__(self, encoding: List[int]):
self.encoding = encoding
self.i = 0
self.j = 0
def next(self, n: int) -> int:
while self.i < len(self.encoding):
if self.encoding[self.i] - self.j < n:
n -= self.encoding[self.i] - self.j
self.i += 2
self.j = 0
else:
self.j += n
return self.encoding[self.i + 1]
return -1
# Your RLEIterator object will be instantiated and called as such:
# obj = RLEIterator(encoding)
# param_1 = obj.next(n)
class RLEIterator {
private int[] encoding;
private int i;
private int j;
public RLEIterator(int[] encoding) {
this.encoding = encoding;
}
public int next(int n) {
while (i < encoding.length) {
if (encoding[i] - j < n) {
n -= (encoding[i] - j);
i += 2;
j = 0;
} else {
j += n;
return encoding[i + 1];
}
}
return -1;
}
}
/**
* Your RLEIterator object will be instantiated and called as such:
* RLEIterator obj = new RLEIterator(encoding);
* int param_1 = obj.next(n);
*/
class RLEIterator {
public:
RLEIterator(vector<int>& encoding) {
this->encoding = encoding;
}
int next(int n) {
while (i < encoding.size()) {
if (encoding[i] - j < n) {
n -= (encoding[i] - j);
i += 2;
j = 0;
} else {
j += n;
return encoding[i + 1];
}
}
return -1;
}
private:
vector<int> encoding;
int i = 0;
int j = 0;
};
/**
* Your RLEIterator object will be instantiated and called as such:
* RLEIterator* obj = new RLEIterator(encoding);
* int param_1 = obj->next(n);
*/
type RLEIterator struct {
encoding []int
i, j int
}
func Constructor(encoding []int) RLEIterator {
return RLEIterator{encoding, 0, 0}
}
func (this *RLEIterator) Next(n int) int {
for this.i < len(this.encoding) {
if this.encoding[this.i]-this.j < n {
n -= (this.encoding[this.i] - this.j)
this.i += 2
this.j = 0
} else {
this.j += n
return this.encoding[this.i+1]
}
}
return -1
}
/**
* Your RLEIterator object will be instantiated and called as such:
* obj := Constructor(encoding);
* param_1 := obj.Next(n);
*/
class RLEIterator {
private encoding: number[];
private i: number;
private j: number;
constructor(encoding: number[]) {
this.encoding = encoding;
this.i = 0;
this.j = 0;
}
next(n: number): number {
while (this.i < this.encoding.length) {
if (this.encoding[this.i] - this.j < n) {
n -= this.encoding[this.i] - this.j;
this.i += 2;
this.j = 0;
} else {
this.j += n;
return this.encoding[this.i + 1];
}
}
return -1;
}
}
/**
* Your RLEIterator object will be instantiated and called as such:
* var obj = new RLEIterator(encoding)
* var param_1 = obj.next(n)
*/