# 900.RLE Iterator

**900.RLE iterator**

题目难度 Medium

> 编写一个遍历游程编码序列的迭代器。 迭代器由 RLEIterator(int\[] A) 初始化，其中 A 是某个序列的游程编码。更具体地，对于所有偶数 i，A\[i] 告诉我们在序列中重复非负整数值 A\[i + 1] 的次数。 迭代器支持一个函数：next(int n)，它耗尽接下来的 n 个元素（n >= 1）并返回以这种方式耗去的最后一个元素。如果没有剩余的元素可供耗尽，则 next 返回 -1 。 例如，我们以 A = \[3,8,0,9,2,5] 开始，这是序列 \[8,8,8,5,5] 的游程编码。这是因为该序列可以读作 “三个八，零个九，两个五”。

```
示例：

输入：["RLEIterator","next","next","next","next"], [[[3,8,0,9,2,5]],[2],[1],[1],[2]]
输出：[null,8,8,5,-1]
解释：
RLEIterator 由 RLEIterator([3,8,0,9,2,5]) 初始化。
这映射到序列 [8,8,8,5,5]。
然后调用 RLEIterator.next 4次。

.next(2) 耗去序列的 2 个项，返回 8。现在剩下的序列是 [8, 5, 5]。

.next(1) 耗去序列的 1 个项，返回 8。现在剩下的序列是 [5, 5]。

.next(1) 耗去序列的 1 个项，返回 5。现在剩下的序列是 [5]。

.next(2) 耗去序列的 2 个项，返回 -1。 这是由于第一个被耗去的项是 5，
但第二个项并不存在。由于最后一个要耗去的项不存在，我们返回 -1。
 

提示：

0 <= A.length <= 1000
A.length 是偶数。
0 <= A[i] <= 10^9
每个测试用例最多调用 1000 次 RLEIterator.next(int n)。
每次调用 RLEIterator.next(int n) 都有 1 <= n <= 10^9 。
```

方法： 方法比较简单，判断n是否大于A\[0],如果是，则n-=A\[0]，然后A向量删掉前两个元素，继续循环判断，最后返回n删除的元素。

```
class RLEIterator {
private:
    vector<int >a;
public:
    RLEIterator(vector<int> A) {
        a=A;
    }
    
    int next(int n) {
        int result;
        
        while(n>0)
        {
            if(a.size()==0)
            {
                return -1;
            }

            if(n >=a[0] )
            {
                n -=a[0];
                result = a[1];
                a.erase(a.begin());
                a.erase(a.begin());
                
            }
            else 
            {
                a[0] -= n;
                n=0;
                result =a[1];
            }
          
        }
        
        return result;
    }
};

/**
 * Your RLEIterator object will be instantiated and called as such:
 * RLEIterator obj = new RLEIterator(A);
 * int param_1 = obj.next(n);
 */
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dfine.gitbook.io/leetcode/900.rle_iterator.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
