Write an iterator that iterates through a run-length encoded sequence.

The iterator is initialized by RLEIterator(int[] A), where A is a run-length encoding of some sequence.  More specifically, for all even iA[i] tells us the number of times that the non-negative integer value A[i+1] is repeated in the sequence.

The iterator supports one function: next(int n), which exhausts the next n elements (n >= 1) and returns the last element exhausted in this way.  If there is no element left to exhaust, next returns -1 instead.

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:

  1. 0 <= A.length <= 1000
  2. A.length is an even integer.
  3. 0 <= A[i] <= 10^9
  4. There are at most 1000 calls to RLEIterator.next(int n) per test case.
  5. Each call to RLEIterator.next(int n) will have 1 <= 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的更多相关文章

  1. LC 900. RLE Iterator

    Write an iterator that iterates through a run-length encoded sequence. The iterator is initialized b ...

  2. [LeetCode] 900. RLE Iterator RLE迭代器

    Write an iterator that iterates through a run-length encoded sequence. The iterator is initialized b ...

  3. leetcode 900. RLE Iterator

    Write an iterator that iterates through a run-length encoded sequence. The iterator is initialized b ...

  4. 【LeetCode】900. RLE Iterator 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/rle-itera ...

  5. 【leetcode】900. RLE Iterator

    题目如下: 解题思路:非常简单的题目,直接递归就行了. 代码如下: class RLEIterator(object): def __init__(self, A): ""&quo ...

  6. [Swift]LeetCode900. RLE 迭代器 | RLE Iterator

    Write an iterator that iterates through a run-length encoded sequence. The iterator is initialized b ...

  7. RLE Iterator LT900

    Write an iterator that iterates through a run-length encoded sequence. The iterator is initialized b ...

  8. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

  9. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

随机推荐

  1. Maven 学习笔记(一) 基础环境搭建

    在Java的世界里,项目的管理与构建,有两大常用工具,一个是Maven,另一个是Gradle,当然,还有一个正在淡出的Ant.Maven 和 Gradle 都是非常出色的工具,排除个人喜好,用哪个工具 ...

  2. 阈值分割与XLD轮廓拼接——第4讲

    一.阈值分割 阈值分割算子众多: threshold :这是最基本最简单的阈值算子. binary_threshold :它是自动阈值算子,自动选出暗(dark)的区域,或者自动选出亮(light)的 ...

  3. cuda使用

    import torchx = torch.randn(2, 3)x = x.cuda() print (x)#最简单的cuda使用,但是感觉好慢啊

  4. sqoop 安装使用

    安装配置: 1.将sqoop-1.4.4.tar.gz 上传到/usr/local/ 2.解压 tar -zxvf sqoop-1.4.4.tar.gz 3.配置 vim /etc/profile 在 ...

  5. 2018.08.22 codves2370 小机房的树(lca+树上差分)

    传送门 一道板子题. 直接树链剖分维护树上lca然后差分就行了. 代码: #include<bits/stdc++.h> #define N 50005 #define lc (p< ...

  6. hdu-1133

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1133 思路:有m个人拿50元的纸币,n个人拿100元的纸币门票价格是50元,要求每个售票员遇到100元 ...

  7. 36 The Benefits of Marriage 结婚的益处

    36 The Benefits of Marriage 结婚的益处 ①Being sociable looks like a good way to add years to your life.Re ...

  8. simpson法计算arctan(1)-即pi/4

    对1/(1+x^2) 进行0到1的积分即使pi/4; 采用simpson方法 Func<double,double> func=(x)=>{ return 1/(1+ Math.Po ...

  9. try-catch+thows异常范围说明

    方式一: CatalogPO deleteTarget = null; /** 查询是否存在 **/ deleteTarget = catalogMapper.findByCatalogId(cata ...

  10. c语言学生信息管理系统-学习结构体

    #include<stdio.h> #include<stdlib.h> //结构体可以存放的学生信息最大个数,不可变变量 ; //学生信息结构体数组,最多可以存放100个学生 ...