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. 【原创】Silverlight之TextBox的LostFocus、GotFocus事件

    <TextBox x:Name="txtCount" Width="200" Height="35" GotFocus="t ...

  2. ubuntu 开机自动挂载nfs服务器上的home分区

    通过‘fstab’也可以配置 NFS 和 SMB 的共享目录.由于涉及到的可选项很重要,并且需要了解一些协议的工作情况,您得先阅读 Samba 和 NFS . 基本语法和本地介质相差不是很多.条目中的 ...

  3. BZOJ1106[POI2007]立方体大作战tet - 树状数组

    描述 一个叫做立方体大作战的游戏风靡整个Byteotia.这个游戏的规则是相当复杂的,所以我们只介绍他的简单规则:给定玩家一个有2n个元素的栈,元素一个叠一个地放置.这些元素拥有n个不同的编号,每个编 ...

  4. 11月11日光棍节考试总结hhh

    好吧,第一题字符串裸栈就能A 第二题字典序没调完,先写的第一题和第三题暴力,第二题读题感觉自己不会写,其实也能写出来,浪费了好多时间 第三题DP大概是写的暴力,原本可以搞到20分,要交的时候发现自己少 ...

  5. Microsoft DirectX SDK 2010 版本下载

    Microsoft DirectX SDK 2010 版本下载 Version:Date Published:9.29.19626/7/2010File name:File size:DXSDK_Ju ...

  6. input不能输入中文

    <input type="text" oninput="this.value = this.value.replace(/[\u4e00-\u9fa5d]/g, ' ...

  7. Python 析构方法__del__

    class Car: def __init__(self): print('---ok---') def __del__(self): print('----deconstrcut-------') ...

  8. js模态窗口返回值(table)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. c#中关于变量声明那么点事

    class MyVar { /* * 基于安全的考虑,c#变量的初始化有一定的要求 * 1.所有的局部变量在被显示的初始化之前,都会被编译器当作未初始化,然后抛出编译期出错; * 2.所有的字段级变量 ...

  10. python 实现排列组合

    1.python语言简单.方便,其内部可以快速实现排列组合算法,下面做简单介绍. 2.一个列表数据任意组合 2.1主要是利用自带的库 #_*_ coding:utf-8 _*_ #__author__ ...