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:

  1. Input: ["RLEIterator","next","next","next","next"], [[[3,8,0,9,2,5]],[2],[1],[1],[2]]
  2. Output: [null,8,8,5,-1]
  3. Explanation:
  4. RLEIterator is initialized with RLEIterator([3,8,0,9,2,5]).
  5. This maps to the sequence [8,8,8,5,5].
  6. RLEIterator.next is then called 4 times:
  7.  
  8. .next(2) exhausts 2 terms of the sequence, returning 8. The remaining sequence is now [8, 5, 5].
  9.  
  10. .next(1) exhausts 1 term of the sequence, returning 8. The remaining sequence is now [5, 5].
  11.  
  12. .next(1) exhausts 1 term of the sequence, returning 5. The remaining sequence is now [5].
  13.  
  14. .next(2) exhausts 2 terms, returning -1. This is because the first term exhausted was 5,
  15. 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]

  1. class RLEIterator {
  2. int index;
  3. int[] A;
  4. public RLEIterator(int[] A) {
  5. this.A = A;
  6. index = 0;
  7. }
  8.  
  9. public int next(int n) {
  10. while (index < A.length && n > A[index]) {
  11. n = n - A[index];
  12. index += 2;
  13. }
  14. if (index >= A.length) return -1;
  15. A[index] = A[index] - n;
  16. return A[index+1];
  17. }
  18. }
  19.  
  20. /**
  21. * Your RLEIterator object will be instantiated and called as such:
  22. * RLEIterator obj = new RLEIterator(A);
  23. * int param_1 = obj.next(n);
  24. */

  

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. 菜刀连接webshell

    中国菜刀,一个非常好用而又强大的webshell,它可不是用来切菜的做饭的道具哦,是一款专业的网站管理软件,大小只有300多KB,真是小巧实用啊!不过被不法分子利用到,就是一个黑站的利器了.我记得以前 ...

  2. SNP问题大集锦

    SNP问题大集锦 [2017-01-19]       最近小编对基因检测很感兴趣,也跟风去测了一下,这一测不要紧,吓得小编几天没睡着觉,这不,检测报告上称小编的减肥能力弱,虽然小编一家都是胖子,唯有 ...

  3. 20172325 2017-2018-2 《Java程序设计》第六周学习总结

    20172325 2017-2018-2 <Java程序设计>第六周学习总结 教材学习内容总结 1.利用[ ]建立一个数组,整列数据可以通过数组名引用,数组中的每个元素则可以通过其在数组中 ...

  4. 80% UI 初学者走过的弯路,你走了几条?

    关于UI 对于初学UI设计的人而言,可能对UI具体是做什么,或者自己是否能顺利转行胜任这样的岗位存在一定的顾虑,今天我们就来重点说说UI是做什么的,以及学UI到有哪些需要避免的弯路. 1.UI设计是做 ...

  5. mysq 日期l查询

    pym=mysql(host = '#', port = 3306, user = '#',passworld='#',database='#') #根据起始和结束时间 charge_sql = 'S ...

  6. Django介绍(3)

    https://www.cnblogs.com/yuanchenqi/articles/5786089.html

  7. Django入门与实践-第17章:保护视图(完结)

    http://127.0.0.1:8000/boards/1/ #boards/views.py from django.contrib.auth.decorators import login_re ...

  8. systemctl自定义service

    应用场景:开启自启动运行脚本/usr/local/manage.sh 一.服务介绍 CentOS 7的服务systemctl脚本存放在:/usr/lib/systemd/,有系统(system)和用户 ...

  9. java中线程和并发面试题

    http://www.cnblogs.com/dolphin0520/p/3932934.html http://www.cnblogs.com/dolphin0520/p/3958019.html ...

  10. Python中的replace方法

    replace 方法:返回根据正则表达式进行文字替换后的字符串的复制. stringObj.replace(rgExp, replaceText) 参数 stringObj必选项.要执行该替换的 St ...