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

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: 0 <= A.length <= 1000
A.length is an even integer.
0 <= A[i] <= 10^9
There are at most 1000 calls to RLEIterator.next(int n) per test case.
Each call to RLEIterator.next(int n) will have 1 <= n <= 10^9.

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

模拟一下。

class RLEIterator {
public:
queue<pair<int,int> > q;
RLEIterator(vector<int> A) {
for (int i = 0; i < A.size()-1; i+= 2) {
int x = A[i];
int y = A[i+1];
//mp[y] = x;// y有x个
q.push({y,x});
}
} int next(int n) {
while (!q.empty() && n > 0) {
auto &x = q.front();
if (x.second >= n) {
x.second -= n;
if (x.second == 0) q.pop();
return x.first;
} else {
n -= x.second;
q.pop();
}
}
return -1;
}
}; /**
* Your RLEIterator object will be instantiated and called as such:
* RLEIterator obj = new RLEIterator(A);
* int param_1 = obj.next(n);
*/

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. OSX: 命令行制作U盘Recovery HD

    使用命令行操作,非常easy,可是操作不当非常危急! 免责声明:假设操作不当造成的数据丢失,本人概不负责. 为什么? 不是有OSX恢复磁盘助理嘛?是的.假设仅仅想使用GUI的软件.能够去苹果官方站点: ...

  2. STM32F4先设置寄存器还是先使能时钟

    http://zhidao.baidu.com/link?url=gdVNuIgLOJcV37QzbCx0IrFip5pskiPQDWpoZayr_xBEe120p4d_iWtrfDl1d4tSFaH ...

  3. TCP是如何保证包的顺序传输

    转自:http://blog.csdn.net/ggxxkkll/article/details/7894112 大家都知道,TCP提供了最可靠的数据传输,它给发送的每个数据包做顺序化(这看起来非常烦 ...

  4. 使用React的static方法实现同构以及同构的常见问题

    代码地址请在github查看,假设有新内容.我会定时更新.也欢迎您star,issue,共同进步 1.我们服务端渲染数据从何而来 1.1 怎样写出同构的组件 服务端生成HTML结构有时候并不完好.有时 ...

  5. 【POJ 2400】 Supervisor, Supervisee(KM求最小权匹配)

    [POJ 2400] Supervisor, Supervisee(KM求最小权匹配) Supervisor, Supervisee Time Limit: 1000MS   Memory Limit ...

  6. 浅谈C/C++堆栈指引——C/C++堆栈

    C/C++堆栈指引 Binhua Liu 前言 我们经常会讨论这种问题:什么时候数据存储在飞鸽传书堆栈(Stack)中.什么时候数据存储在堆(Heap)中.我们知道.局部变量是存储在堆栈中的.debu ...

  7. 解析spark RDD

    RDD是spark抽象的基石,可以说整个spark编程就是对RDD进行的操作   RDD是弹性的分布式数据集,它是只读的,可分区的,这个数据集的全部或者部分数据可以缓存在内存中,在多次计算间重用.所谓 ...

  8. secureCrt Linux 文件传输

    1.在secureCRT终端下输入rz命令,查看Linux是否安装rz文件传输服务 如果提示未安装则先安装rz服务:安装命令:   apt-get install lrzsz 2.进入要上传的目的文件 ...

  9. CAP理论学习

    CAP理论是对分布式系统的3个特性所下的一个定性的结论,可用于指导分布式系统的设计. CAP理论断言任何基于网络的数据共享系统,最多只能满足数据一致性.可用性.分区容忍性三要素中的两个要素. 在英语中 ...

  10. 对象复制帮助类---DeepCopy

    有的时候我们在对一个引用类型的对象进行传递操作的时候希望不要直接修改传递过来的对象,而是复制出一份来操作的时候就可以用下面的类进行复制 sing System.IO; using System.Run ...