ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

第58天(中等题 数据结构)

2026/9/30 5:57:35 拓冰建站 浏览量
第58天(中等题 数据结构)

打卡第五十八天
2道中等题
image

题目:
image

思路:
image

代码:

class Solution {
public:vector<string> buildArray(vector<int>& target, int n) {  // n 是可用数字范围 [1, n]vector<string> ans;  int mx = target.back();  // 目标数组的最大值int i = 0;  // 指向 target 当前元素的索引for (int x = 1; x <= mx; x++) {ans.push_back("Push");  // 先入栈if (x == target[i]) {  // 当前数字是目标数组需要的元素i++;               // 移动到目标数组的下一个元素} else {               ans.push_back("Pop");  // 移除该数字}}return ans;}
};

耗时≈一小时 明天继续