900. RLE Iterator
Write an iterator that iterates through a run-length encoded sequence.
The iterator is initialized by
RLEIterator(int[] A), whereAis a run-length encoding of some sequence. More specifically, for all eveni,A[i]tells us the number of times that the non-negative integer valueA[i+1]is repeated in the sequence.The iterator supports one function:
next(int n), which exhausts the nextnelements (n >= 1) and returns the last element exhausted in this way. If there is no element left to exhaust,nextreturns-1instead.For example, we start with
A = [3,8,0,9,2,5], which is a run-length encoding of the sequence[8,8,8,5,5]. This is because the sequence can be read as "three eights, zero nines, two fives".
Example 1:
Input: ["RLEIterator","next","next","next","next"], [[[3,8,0,9,2,5]],[2],[1],[1],[2]]
Output: [null,8,8,5,-1]
Explanation:
RLEIterator is initialized with RLEIterator([3,8,0,9,2,5]).
This maps to the sequence [8,8,8,5,5].
RLEIterator.next is then called 4 times: .next(2) exhausts 2 terms of the sequence, returning 8. The remaining sequence is now [8, 5, 5]. .next(1) exhausts 1 term of the sequence, returning 8. The remaining sequence is now [5, 5]. .next(1) exhausts 1 term of the sequence, returning 5. The remaining sequence is now [5]. .next(2) exhausts 2 terms, returning -1. This is because the first term exhausted was 5,
but the second term did not exist. Since the last term exhausted does not exist, we return -1.
Note:
0 <= A.length <= 1000A.lengthis an even integer.0 <= A[i] <= 10^9- There are at most
1000calls toRLEIterator.next(int n)per test case.- Each call to
RLEIterator.next(int n)will have1 <= n <= 10^9.
Approach #1: Array. [Java]
class RLEIterator {
int index;
int[] A;
public RLEIterator(int[] A) {
this.A = A;
index = 0;
}
public int next(int n) {
while (index < A.length && n > A[index]) {
n = n - A[index];
index += 2;
}
if (index >= A.length) return -1;
A[index] = A[index] - n;
return A[index+1];
}
}
/**
* Your RLEIterator object will be instantiated and called as such:
* RLEIterator obj = new RLEIterator(A);
* int param_1 = obj.next(n);
*/
Refereence:
https://leetcode.com/problems/rle-iterator/discuss/168294/Java-Straightforward-Solution-O(n)-time-O(1)-space
900. RLE Iterator的更多相关文章
- LC 900. RLE Iterator
Write an iterator that iterates through a run-length encoded sequence. The iterator is initialized b ...
- [LeetCode] 900. RLE Iterator RLE迭代器
Write an iterator that iterates through a run-length encoded sequence. The iterator is initialized b ...
- leetcode 900. RLE Iterator
Write an iterator that iterates through a run-length encoded sequence. The iterator is initialized b ...
- 【LeetCode】900. RLE Iterator 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/rle-itera ...
- 【leetcode】900. RLE Iterator
题目如下: 解题思路:非常简单的题目,直接递归就行了. 代码如下: class RLEIterator(object): def __init__(self, A): ""&quo ...
- [Swift]LeetCode900. RLE 迭代器 | RLE Iterator
Write an iterator that iterates through a run-length encoded sequence. The iterator is initialized b ...
- RLE Iterator LT900
Write an iterator that iterates through a run-length encoded sequence. The iterator is initialized b ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
随机推荐
- metasploit渗透测试指南概要整理
一.名词解释 exploit 测试者利用它来攻击一个系统,程序,或服务,以获得开发者意料之外的结果.常见的 有内存溢出,网站程序漏洞利用,配置错误exploit. payload 我们想让被攻击系统执 ...
- 改变父元素的透明度,不影响子元素的透明度—css
1.如果我们给父元素添加opacity:0.4后,子元素的red颜色也变成了0.4的透明度, 例子如下: <!DOCTYPE html> <html> <head> ...
- Redis数据持久化
持久化选项 Redis提供了两种不同的持久化方法来将数据存储到硬盘里面.一种方法叫快照(snapshotting),它可以将存在于某一时刻的所有数据都写入硬盘里面.另一种方法叫只追加文件(append ...
- C&C++内存布局
1. Memory Layout of a C Program http://codingfreak.blogspot.com/2012/03/memory-layout-of-c-program-p ...
- css之颜色篇
app多采用浅灰#f5f5f5 白色一般用white,如果觉得白太直接了,可以加一点点灰,#fefefe, 这种情况下搭配#e4e4e4的浅灰边框最合适.
- java jvm入门 jvm什么意思 jvm调优
接下来我会给大家讲解 国内最顶尖的jvm技术 不服来战
- JavaScript 的数据类型及其检测
JavaScript 有几种类型的值? Javascript 有两种数据类型,分别是基本数据类型和引用数据类型.其中基本数据类型包括 Undefined.Null.Boolean.Number.Str ...
- 用pyqt5做一个能python程序能插入图片的ide
之前只是放到github上了,现在一想应该开源,大家想继续做好这个ide的都能从这里起步. #注意在.py文件相同目录下放一个1.png做测试图片 #本质就是用html来实现图片 #写在前面的话:这个 ...
- KOBAS
1. What is KOBAS 3.0? KOBAS (KEGG Orthology Based Annotation System) is a web server for gene/protei ...
- c++中如何定义编译期间常量,即这个常量可以用于定义数组下标
在c++中,类里面的成员变量不仅仅可以被const修饰,还可以被static const修饰,此时一个内建类型(如int ,char ,long等)的static const 可以看做是一个编译期间的 ...