Leecode RLE 迭代器 _900

编写一个遍历游程编码序列的迭代器。

Leecode 900 号问题
迭代器由 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

自己写的代码,感觉有点垃圾

#define LOCAL
#include<stdio.h>
#include<iostream>
#include <vector>
#include <queue>
using namespace std;

class RLEIterator {
public:

    vector<int> v;

    long total = 0;
    int start  = 0;
    RLEIterator(vector<int> A) {
        this->v.clear();
        vector<int>::iterator ite;
        int o = 0;
        for (ite = A.begin(); ite != A.end(); ite++){
            v.push_back( * ite);
            if (!(o++ & 1)){
                this->total += *ite;
            }
        }
    }

    int next(int n) {

        vector<int> & v = this->v;
        int num = n;
        int i = 0;
        vector<int>::iterator ite;
        for (ite = v.begin() + this->start; ite != v.end() && num > *ite; ite+=2){
            num -= *ite;
            this->total -= *ite;
            this->start += 2;
        }
        v[this->start] -= num ;
        this->total -= num;

        int out = v[this->start + 1];
        if ( (v[this->start]) == 0){
            this->start += 2; 
        }
        if ( this->total <= 0 ) out = -1;
        return out;
    }
};

// 测试
int main(){
    #ifdef LOCAL
        freopen("in.txt", "r", stdin);
    #endif
    vector<int> v;
    int num;
    while(scanf("%d", &num) != EOF){
        v.push_back(num);   
    } 
    RLEIterator ite(v);
    printf("%d\n", ite.next(2));
        printf("%d\n", ite.next(1));
            printf("%d\n", ite.next(1));
                printf("%d\n", ite.next(2));    

    return 0;
}
    原文作者:游程编码问题
    原文地址: https://blog.csdn.net/liuYinXinAll/article/details/82629124
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞