LeetCode 1441 - 用栈操作构建数组(周赛)

题目描述

1441. 用栈操作构建数组

解法:(C++)

class Solution {
public:
    vector<string> buildArray(vector<int>& target, int n) {
        queue<int> list;
        vector<string> res;
        string op1 = "Push", op2 = "Pop";
        for(int i=1;i<=n;i++)
            list.push(i);
        for(int i: target)
        {
            if(i==list.front())
            {
                res.push_back(op1);
                list.pop();
                continue;
            }
            while(i!=list.front())
            {
                res.push_back(op1);
                res.push_back(op2);
                list.pop();
            }
            res.push_back(op1);
            list.pop();
        }
        return res;
    }
};

你可能感兴趣的:(题解)