同步操作将从 doocs/leetcode 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
我们可以使用游程编码(即 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
次 我们定义两个指针 $i$ 和 $j$,其中指针 $i$ 指向当前读取的游程编码,指针 $j$ 指向当前读取的游程编码中的第几个字符。初始时 $i = 0$, $j = 0$。
每次调用 next(n)
时,我们判断当前游程编码中剩余的字符数 $encoding[i] - j$ 是否小于 $n$,若是,则将 $n$ 减去 $encoding[i] - j$,并将 $i$ 加 $2$,$j$ 置为 $0$,然后继续判断下一个游程编码;若不是,则将 $j$ 加 $n$,并返回 $encoding[i + 1]$。
若 $i$ 超出了游程编码的长度,依然没有返回值,则说明没有剩余的元素要耗尽,返回 $-1$。
时间复杂度 $O(n + q)$,空间复杂度 $O(n)$。其中 $n$ 是游程编码的长度,而 $q$ 是调用 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)
*/
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。