https://leetcode-cn.com/problems/build-an-array-with-stack-operations/
思路:假设当前需要从
l
i
s
t
list
list中读取的数字为
v
v
v,且轮到
t
a
r
g
e
t
target
target的第
i
i
i个数字了,那么就拿
v
v
v与
t
a
r
g
e
t
[
i
]
target[i]
target[i]比较,如果不相等,就令
v
v
v自增,并且做
p
u
s
h
、
p
o
p
push、pop
push、pop操作,否则做
p
u
s
h
push
push操作即可。
class Solution {
public:
vector<string> buildArray(vector<int>& target, int n) {
int siz=target.size();
int cur=1;
vector<string> ans;
for(int i=0;i<siz;i++){
while(cur++<target[i])
ans.push_back("Push"),ans.push_back("Pop");
ans.push_back("Push");
}
return ans;
}
};