队列及栈相关题目的实现

1. 可查询最值的栈

定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数。

使用两个栈即可

class Solution {
public:
    void push(int value) {
        baseStack.push(value);
        if (!minStack.empty())
            minStack.push(std::min(minStack.top(), value));
        else
            minStack.push(value);
    }
    void pop() {
        if (!baseStack.empty()){
            baseStack.pop();
            minStack.pop();
        }
    }
    int top() {
        return baseStack.top();
    }
    int min() {
        return minStack.top();
    }

private:
    stack<int> baseStack;
    stack<int> minStack;
};

2. 双栈队列

编写一个类,只能用两个栈结构实现队列,支持队列的基本操作(push,pop)。
给定一个操作序列ope及它的长度n,其中元素为正数代表push操作,为0代表pop操作,保证操作序列合法且一定含pop操作,请返回pop的结果序列。
测试样例:
[1,2,3,0,4,0],6
返回:[1,2]

class TwoStack {
public:
    vector<int> twoStack(vector<int> ope, int n) {
        // write code here 
        vector<int> res;
        for (int i = 0; i != n; i++){
            if (ope[i] > 0)
                QPush(ope[i]);
            else
                res.push_back(QPop());
        }

        return res;
    }

    void QPush(int value){
        pushStack.push(value);
    }

    int QPop(){
        int top;
        if (!popStack.empty()){
            top = popStack.top();
            popStack.pop();
        }
        else{
            while (!pushStack.empty()){
                popStack.push(pushStack.top());
                pushStack.pop();
            }
            if (!popStack.empty()){
                top = popStack.top();
                popStack.pop();
            }
        }
        return top;
    }

private:
    stack<int> pushStack;
    stack<int> popStack;
};

3. 栈的反转

请编写一个程序,按升序对栈进行排序(即最大元素位于栈顶),要求最多只能使用一个额外的栈存放临时数据,但不得将元素复制到别的数据结构中。
给定一个int[] numbers(C++中为vector),其中第一个元素为栈顶,请返回排序后的栈。请注意这是一个栈,意味着排序过程中你只能访问到第一个元素。
测试样例:
[1,2,3,4,5]
返回:[5,4,3,2,1]

每次将最小元素放到另一个栈的栈底即可

class TwoStacks {
public:
    vector<int> twoStacksSort(vector<int> numbers) {
        // write code here
        vector<int> helper(numbers.size(), 0);
        int helper_id = 0;
        int id = numbers.size() - 1;
        while (id >= 0){
            int top = numbers[id--];

            if (helper_id == 0)
                helper[helper_id++] = top;
            else{
                int id_tmp = id;
                while (helper_id >= 1 && helper[helper_id - 1] < top){
                    numbers[id++ + 1] = helper[helper_id-- - 1];
                }
                helper[helper_id++] = top;
                while (id > id_tmp){
                    helper[helper_id++] = numbers[id--];
                }
            }
        }

        return helper;
    }
};

4. 双栈排序

实现一个栈的逆序,但是只能用递归函数和这个栈本身的pop操作来实现,而不能自己申请另外的数据结构。
给定一个整数数组A即为给定的栈,同时给定它的大小n,请返回逆序后的栈。
测试样例:
[4,3,2,1],4
返回:[1,2,3,4]

依次将栈底元素放到相应的位置上

class StackReverse {
public:
    vector<int> reverseStack(vector<int> A, int n) {
        // write code here
        int depth = n - 1;
        reverseStack(A, n, depth);
        return A;
    }

private:
    bool reverseStack(vector<int> & A, int n, int depth) {
        // write code here
        if (n <= 1)
            return true;

        int last = getLast(A, n);
        A[depth--] = last;
        auto res = reverseStack(A, n - 1, depth);      
        return res;
    }

private:
    // 取出栈底元素, 并删除
    int getLast(vector<int> & A, int n){
        if (n <= 1)
            return A[0];

        int top = A[n - 1];
        int last = getLast(A, n - 1);
        A[n - 2] = top;
        return last;
    }
};

5. 滑动窗口

有一个整型数组 arr 和一个大小为 w 的窗口从数组的最左边滑到最右边,窗口每次向右边滑一个位置。 返回一个长度为n-w+1的数组res,res[i]表示每一种窗口状态下的最大值。 以数组为[4,3,5,4,3,3,6,7],w=3为例。因为第一个窗口[4,3,5]的最大值为5,第二个窗口[3,5,4]的最大值为5,第三个窗口[5,4,3]的最大值为5。第四个窗口[4,3,3]的最大值为4。第五个窗口[3,3,6]的最大值为6。第六个窗口[3,6,7]的最大值为7。所以最终返回[5,5,5,4,6,7]。
给定整形数组arr及它的大小n,同时给定w,请返回res数组。保证w小于等于n,同时保证数组大小小于等于500。
测试样例:
[4,3,5,4,3,3,6,7],8,3
返回:[5,5,5,4,6,7]

维护一个递减的deque 双端队列

class SlideWindow {
public:
    // 维护一个递减的deque 双端队列
    vector<int> slide(vector<int> arr, int n, int w) {
        // write code here
        deque<int> myqueue;
        vector<int> res(n - w + 1, 0);
        for (int i = 0; i < n; i++){
            if (myqueue.empty() || arr[myqueue.back()] > arr[i]){
                myqueue.push_back(i);
            }
            else{
                while (!myqueue.empty() && arr[myqueue.back()] <= arr[i]){
                    myqueue.pop_back();
                }
                myqueue.push_back(i);
            }

            if (!myqueue.empty() && (i - myqueue.front() + 1 > w))
                myqueue.pop_front();

            if (i - w + 1 >= 0)
                res[i - w + 1] = arr[myqueue.front()];
        }
        return res;
    }
};

6. 数组变树

对于一个没有重复元素的整数数组,请用其中元素构造一棵MaxTree,MaxTree定义为一棵二叉树,其中的节点与数组元素一一对应,同时对于MaxTree的每棵子树,它的根的元素值为子树的最大值。现有一建树方法,对于数组中的每个元素,其在树中的父亲为数组中它左边比它大的第一个数和右边比它大的第一个数中更小的一个。若两边都不存在比它大的数,那么它就是树根。请设计O(n)的算法实现这个方法。
给定一个无重复元素的数组A和它的大小n,请返回一个数组,其中每个元素为原数组中对应位置元素在树中的父亲节点的编号,若为根则值为-1。
测试样例:
[3,1,4,2],4
返回:[2,0,-1,2]

这里写图片描述

class MaxTree {
public:
    vector<int> buildMaxTree(vector<int> A, int n) {
        // write code here
        vector<int> left(n, 0), right(n, 0), res(n, 0);
        stack<int> lmax, rmax;
        getMax(A, n, lmax, left, true);
        getMax(A, n, rmax, right, false);

        for (int i = 0; i < n; i++){
            if (left[i] == -1 && right[i] == -1)
                return vector<int>{-1};
            else if (left[i] == -1)
                res[i] = right[i];
            else if (right[i] == -1)
                res[i] = left[i];
            else if (right[i] == -2 && left[i] == -2)
                res[i] = -1;
            else{
                res[i] = (A[left[i]] < A[right[i]]) ? left[i] : right[i];
            }
        }
        return res;
    }

private:
    void getMax(vector<int> & A, int n, stack<int> & lmax, vector<int> & left, bool isLeft){
        int i = isLeft ? 0 : n - 1;
        int beg = isLeft ? 0 : n - 1;
        int max_id = beg;

        for (; isLeft ? i < n : i >= 0; isLeft ? i++ : i--){
            if (i == beg){
                lmax.push(i);
                left[beg] = -1;
            }
            else{
                if (A[i] < A[lmax.top()]){
                    left[i] = lmax.top();
                    lmax.push(i);
                }
                else{
                    while (!lmax.empty() && A[i] >= A[lmax.top()])
                        lmax.pop();
                    left[i] = lmax.empty() ? -1 : lmax.top();
                    lmax.push(i);  

                    if (A[max_id] < A[i])
                        max_id = i;
                }
            }
        }
        left[max_id] = -2;
    }
};
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值