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. SqlDataHelper

    using System;using System.Data;using System.Configuration;using System.Linq;using System.Web;using S ...

  2. 开发中常遇到的Python陷阱和注意点-乾颐堂

    最近使用Python的过程中遇到了一些坑,例如用datetime.datetime.now()这个可变对象作为函数的默认参数,模块循环依赖等等. 在此记录一下,方便以后查询和补充. 避免可变对象作为默 ...

  3. 我的UI启蒙之路

    为什么叫UI启蒙之路呢? 我没有学过美术,也不懂设计,但是有的时候也许就是一种命中注定吧,让我知道了UI,并且一发不可收拾的爱上了它. 具体情况是这样的: 我毕业于电力学校,是一名不折不扣的工科生,专 ...

  4. c++日志练习

    使用ostream流创建写入log日志文件 使用宏 配置文件大小和间隔时间  当创建文件时间间隔或文件大小大于指定数字 则创建新文件 文件名由时间自动命名 /********************* ...

  5. 【Maven】Nexus(Maven仓库私服)下载与安装

    Nexus介绍 Nexus 是Maven仓库管理器,如果你使用Maven,你可以从Maven中央仓库 下载所需要的构件(artifact),但这通常不是一个好的做法,你应该在本地架设一个Maven仓库 ...

  6. event对象的理解

    0.给对象绑定事件准确的说是给对象事件绑定事件函数 1.event:事件对象,当一个事件发生的时候,和当前这个对象发生的事件有关的信息都会被i临时保存到event对象中 2.event对象必须在一个事 ...

  7. 管理型交换机IEEE 802.1Q VLAN设置应用实例

    转载标注: IEEE802.1Q 我粗略理解为对交换机入口规则和出口规则设置,入口打上VID,设置从哪些口可以出去,并且是否带标签. 一 VLAN的概念 VLAN(Virtual Local Area ...

  8. 201.09.22 除虫药水(线性dp)

    描述 在十年前,除虫是十分艰苦的工作.那时,使用普通药水进行除虫的效果极差,在一片苹果 林中使用后除掉的虫仅为极小一部分. 比如说,Bugs 镇共有 N 片苹果林,对第i 片使用普通药水可以除掉 ai ...

  9. 45 The Effect of External Rewards on Behavior 外界奖励对行为的影响

    The Effect of External Rewards on Behavior 外界奖励对行为的影响 ①Psychologists take opposing views on how exte ...

  10. CentOS7中实用的命令总结

    一:软件安装配置方面 这里总结一下对个人很实用的功能 1.查询rpm包的依赖哪些文件:rpm -qpR xx.rpm,然后rpm会分析此包的文件依赖性 2.查询系统中安装了哪些rpm包:rpm -qa ...