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 i, A[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".

  1. Example 1:
  2. Input: ["RLEIterator","next","next","next","next"], [[[3,8,0,9,2,5]],[2],[1],[1],[2]]
  3. Output: [null,8,8,5,-1]
  4. Explanation:
  5. RLEIterator is initialized with RLEIterator([3,8,0,9,2,5]).
  6. This maps to the sequence [8,8,8,5,5].
  7. RLEIterator.next is then called 4 times:
  8. .next(2) exhausts 2 terms of the sequence, returning 8. The remaining sequence is now [8, 5, 5].
  9. .next(1) exhausts 1 term of the sequence, returning 8. The remaining sequence is now [5, 5].
  10. .next(1) exhausts 1 term of the sequence, returning 5. The remaining sequence is now [5].
  11. .next(2) exhausts 2 terms, returning -1. This is because the first term exhausted was 5,
  12. but the second term did not exist. Since the last term exhausted does not exist, we return -1.
  13. Note:
  14. 0 <= A.length <= 1000
  15. A.length is an even integer.
  16. 0 <= A[i] <= 10^9
  17. There are at most 1000 calls to RLEIterator.next(int n) per test case.
  18. Each call to RLEIterator.next(int n) will have 1 <= n <= 10^9.

题意:每次取n个数,返回这n个数,最后的那一个。

模拟一下。

  1. class RLEIterator {
  2. public:
  3. queue<pair<int,int> > q;
  4. RLEIterator(vector<int> A) {
  5. for (int i = 0; i < A.size()-1; i+= 2) {
  6. int x = A[i];
  7. int y = A[i+1];
  8. //mp[y] = x;// y有x个
  9. q.push({y,x});
  10. }
  11. }
  12. int next(int n) {
  13. while (!q.empty() && n > 0) {
  14. auto &x = q.front();
  15. if (x.second >= n) {
  16. x.second -= n;
  17. if (x.second == 0) q.pop();
  18. return x.first;
  19. } else {
  20. n -= x.second;
  21. q.pop();
  22. }
  23. }
  24. return -1;
  25. }
  26. };
  27. /**
  28. * Your RLEIterator object will be instantiated and called as such:
  29. * RLEIterator obj = new RLEIterator(A);
  30. * int param_1 = obj.next(n);
  31. */

leetcode 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. LC 900. RLE Iterator

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

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

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

  4. 【leetcode】900. RLE Iterator

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

  5. 900. RLE Iterator

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

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

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

  7. [LeetCode] 281. Zigzag Iterator 之字形迭代器

    Given two 1d vectors, implement an iterator to return their elements alternately. Example: Input: v1 ...

  8. [LeetCode] 284. Peeking Iterator 瞥一眼迭代器

    Given an Iterator class interface with methods: next() and hasNext(), design and implement a Peeking ...

  9. [LeetCode#281] Zigzag Iterator

    Problem: Given two 1d vectors, implement an iterator to return their elements alternately. For examp ...

随机推荐

  1. 阿里巴巴 DevOps 转型后的运维平台建设

    原文:http://www.sohu.com/a/156724220_262549 本文转载自公众号「DevOps 时代」,高效运维社区致力于陪伴您的职业生涯,与您一起愉快的成长. 作者简介: 陈喻( ...

  2. Eclipse配色利器

    1 http://eclipsecolorthemes.org/  这是官网 2 安装后,window-preferences-general-appearance-color theme 即可找到多 ...

  3. leetcode isPalindrome (回文数判断)

    回文很简单,就是正着读和反着读一样,要判断一个数是否为回文数只需要判断正反两个是不是相等即可. 再往深了想一下,只需要判断从中间分开的两个数一个正读,一个反读相等即可. 代码: class Solut ...

  4. iOS Masonry 抗压缩 抗拉伸

    约束优先级: 在Autolayout中每个约束都有一个优先级, 优先级的范围是1 ~ 1000.创建一个约束,默认的优先级是最高的1000 Content Hugging Priority: 该优先级 ...

  5. 给定一个字符串s,你可以从中删除一些字符,使得剩下的串是一个回文串。如何删除才能使得回文串最长呢? 输出需要删除的字符个数。

    // ConsoleApplication1.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> ...

  6. 解决google登录界面input输入框颜色不正确问题

    加入以下样式: input:-webkit-autofill { -webkit-box-shadow: 0 0 0px 1000px #e2e2e2 inset !important; }

  7. 编程算法 - 二叉树的最低公共祖先 代码(C)

    二叉树的最低公共祖先 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 二叉树的最低公共祖先(lowest common ancestor), 首先先序遍 ...

  8. linux关机前同步数据(sync)

    sync,将内存中未更新的数据写入硬盘中.

  9. com.netflix.hystrix.contrib.javanica.exception.FallbackDefinitionException: fallback method wasn't found: serviceError([class java.lang.String]) 异常

    在使用spring cloud 的 Hystrix 后可能会遇到 如下截图错误: 后台代码如下: 找了好一会经过分析参数方法和原方法参数步一致造成: 修改后代码如下:

  10. Android 热门技术干货

    http://mp.weixin.qq.com/s?__biz=MzIwMzYwMTk1NA==&mid=2247484939&idx=1&sn=d1871b09de55ca6 ...