[Codificación de longitud de ejecución Leetcode C ++] 900. RLE Iterator 900. RLE Iterator

900. Iterador
RLE 900. Iterador RLE

Inserte la descripción de la imagen aquí

class RLEIterator {
    
    
public:
    deque<int> times, nums;
    RLEIterator(vector<int>& A) {
    
    
        for(int ii = 0; ii < A.size(); ii+=2) {
    
    
            times.push_back(A[ii]);
            nums.push_back(A[ii + 1]);
        }
    }
    
    int next(int n) {
    
    
        while(!times.empty() && times.front() < n) {
    
    
            n -= times.front();
            times.pop_front();
            nums.pop_front();
        }
        if(times.empty()) return -1;
        times.front() -= n;
        return nums.front();
    }
};

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

Supongo que te gusta

Origin blog.csdn.net/m0_37454852/article/details/113663025
Recomendado
Clasificación