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.

Runtime: 95 ms, faster than 82.10% of Java online submissions for RLE Iterator.

class RLEIterator {
private Queue<Integer> q = new ArrayDeque<>();
private int cnt = -;
private int value = -;
public RLEIterator(int[] A) {
for(int i=; i<A.length; i++){
q.add(A[i]);
}
} public int next(int n) {
if(cnt == -){
if(q.isEmpty()) {
return -;
}else {
cnt = q.peek();q.poll();
value = q.peek(); q.poll();
}
}
if(cnt >= n){
cnt -= n;
return value;
}else {
n -= cnt;
cnt = -;
}
while(!q.isEmpty()){
cnt = q.peek(); q.poll();
value = q.peek(); q.poll();
if(cnt >= n) {
cnt -= n;
return value;
}else{
n -= cnt;
cnt = -;
}
}
return -;
}
}

LC 900. RLE Iterator的更多相关文章

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

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

  2. 900. RLE Iterator

    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. 11.SpringMVC注解式开发-处理器方法的返回值

    处理器方法的返回值 使用@Controller 注解的处理器的处理器方法,其返回值常用的有四种类型 1.ModelAndView 2.String 3.void 4.自定义类型对象 1.返回Model ...

  2. 6.B+Tree 检索原理

    B+树的创建(索引的创建) 1.比如为phoneNum创建了一个索引,phoneNum这列保存了很多的手机号码 2.索引创建的过程中,会为这些数据进行适当的编码(根据这个数据所在的物理地址),如 36 ...

  3. Linux文件的管理

    创建:touch  vim/vi  echo重定向  cat touch 管理: atime  mtime touch  文件名  //如果文件不存在,不创建文件 删除:mv /tmp find |x ...

  4. 4.安装配置bacula-gui(管理页面)

    1.   安装配置bacula-gui(管理页面) Bacula-gui分为两个部分: Baculum-api可通过OAuth2(开放认证模式)或HTTP认证(用户名和密码)认证提供统一接口服务: B ...

  5. SPI总线的原理与Verilog实现

    转载地址:https://www.cnblogs.com/liujinggang/p/9609739.html 一. 软件平台与硬件平台 软件平台: 1.操作系统:Windows-8.1 2.开发套件 ...

  6. redis——redis的一些核心把握

    redis单线程,为什么比较快 单线程指的是网络请求模块使用了一个线程(所以不需考虑并发安全性),即一个线程处理所有网络请求,其他模块仍用了多个线程.redis能够快速执行的原因有三点: (1) 绝大 ...

  7. C++STL库常用函数用法

    开学就要上OOP了.....感觉十分萌萌哒- -! 整理自<ACM程序设计>,本文为转载(原文地址) 迭代器(iterator) 个人理解就是把所有和迭代有关的东西给抽象出来的,不管是数组 ...

  8. 11 canvas 画布 - 基础

    一.概述 canvas它和其它的HTML5标签的使用基本一致,但是它相当于在浏览器中建立一个画布,可以再这个画布上画图.创建动画甚至是3D游戏.由于canvas要适配不同终端的分辨率,所以尽可能的在标 ...

  9. springboot2.0入门(五)--swagger2接口API构建

    一.特点 代码变,文档变.只需要少量的注解,Swagger 就可以根据代码自动生成 API 文档,很好的保证了文档的时效性. 跨语言性,支持 40 多种语言. Swagger UI 呈现出来的是一份可 ...

  10. Vmware虚拟机 centos7设置固定IP地址

    参考文章:https://www.cnblogs.com/lfhappy/p/10798400.html      https://blog.csdn.net/u014466635/article/d ...