数据结构和算法——栈、队列、堆

1.预备知识
1.1 栈

在这里插入图片描述

S.empty() //1
S.push(10); //2
S.pop(); //3
1.2 队列

在这里插入图片描述

Q.push(5); //1
Q.pop(); //2
Q.push(1); //3
1.3 堆

在这里插入图片描述

图:堆
在这里插入图片描述
图:堆的函数

big_heap.push(1000); //1
i<3 // 2
big_heap.pop(); //3
2.用队列实现栈
2.1 题目描述

请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通队列的全部四种操作(push、top、pop 和 empty)

实现 MyStack 类:
void push(int x) 将元素 x 压入栈顶。
int pop() 移除并返回栈顶元素。
int top() 返回栈顶元素。
boolean empty() 如果栈是空的,返回 true ;否则,返回 false 。

你只能使用队列的基本操作 —— 也就是 push to back、peek/pop from front、size 和 is empty 这些操作。

你所使用的语言也许不支持队列。 你可以使用 list (列表)或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。

2.2 解题思路

在这里插入图片描述

2.3 C++实现
class MyStack {
public:
    /** Initialize your data structure here. */
    MyStack() {

    }
    
    /** Push element x onto stack. */
    void push(int x) {
        queue<int> temp_queue;
        temp_queue.push(x);
        while(!_data.empty()){
            temp_queue.push(_data.front());
            _data.pop();
        }
        while(!temp_queue.empty()){
            _data.push(temp_queue.front());
            temp_queue.pop();
        }
    }
    
    /** Removes the element on top of the stack and returns that element. */
    int pop() {
        int x=_data.front();
        _data.pop();
        return x;
    }
    
    /** Get the top element. */
    int top() {
        return _data.front();
    }
    
    /** Returns whether the stack is empty. */
    bool empty() {
        return _data.empty();
    }
private:
    queue<int> _data;
};
3.用栈实现队列
3.1 题目描述

请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty):

实现 MyQueue 类:
void push(int x) 将元素 x 推到队列的末尾
int pop() 从队列的开头移除并返回元素
int peek() 返回队列开头的元素
boolean empty() 如果队列为空,返回 true ;否则,返回 false

说明:
你只能使用标准的栈操作 —— 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。
你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。

3.2 解题思路

在这里插入图片描述

3.3 C++实现
class MyQueue {
public:
    /** Initialize your data structure here. */
    MyQueue() {

    }
    
    /** Push element x to the back of queue. */
    void push(int x) {
        stack<int> temp_stack;
        while(!_data.empty()){
            temp_stack.push(_data.top());
            _data.pop();
        }
        temp_stack.push(x);
        while(!temp_stack.empty()){
            _data.push(temp_stack.top());
            temp_stack.pop();
        }
    }
    
    /** Removes the element from in front of queue and returns that element. */
    int pop() {
        int x=_data.top();
        _data.pop();
        return x;
    }
    
    /** Get the front element. */
    int peek() {
        return _data.top();
    }
    
    /** Returns whether the queue is empty. */
    bool empty() {
        return _data.empty();
    }
private:
    stack<int> _data;
};
4.最小栈
4.1 题目描述

设计一个支持 push ,pop ,top 操作,并能在常数时间内检索到最小元素的栈。

push(x) —— 将元素 x 推入栈中。
pop() —— 删除栈顶的元素。
top() —— 获取栈顶元素。
getMin() —— 检索栈中的最小元素。
4.2 解题思路
class MinStack {
public:
    /** initialize your data structure here. */
    MinStack() {

    }
    
    void push(int val) {
        _data.push(val);
        if(_min.empty()){
            _min.push(val);
        }
        else{
            if(val>_min.top()){
                val=_min.top();
            }
            _min.push(val);
        }
    }
    
    void pop() {
        _data.pop();
        _min.pop();
    }
    
    int top() {
        return _data.top();
    }
    
    int getMin() {
        return _min.top();
    }
private:
    stack<int> _data;
    stack<int> _min;
};
5.合法的出栈序列
5.1 题目描述

在这里插入图片描述

5.2 解题思路

图1:模拟入栈出栈

在这里插入图片描述
图2:步骤1

在这里插入图片描述

图3:步骤2

在这里插入图片描述

图4:步骤3

在这里插入图片描述
图5:步骤4

5.3 C++实现
#include <iostream>
#include<queue>
#include<stack>
using namespace std;

bool check_is_valid_order(queue<int>& order)
{
    stack<int> S;
    int n = order.size();
    for (int i = 1; i <= n; i++) {
        S.push(i);
        while (!S.empty() && S.top() == order.front()) {
            S.pop();
            order.pop();
        }
    }
    if (!S.empty()) {
        return false;
    }
    return true;
}

int main()
{
    //合法:32541 ;非法:31245
    queue<int> order1, order2;
    bool a1, a2;
    order1.push(3);
    order1.push(2);
    order1.push(5);
    order1.push(4);
    order1.push(1);
    a1 = check_is_valid_order(order1);
    if (a1 == true) {
        cout << "32541合法" << endl;
    }
    else {
        cout << "32541非法" << endl;
    }

    order2.push(3);
    order2.push(1);
    order2.push(2);
    order2.push(4);
    order2.push(5);
    a2 = check_is_valid_order(order2);
    if (a2 == true) {
        cout << "31245合法" << endl;
    }
    else {
        cout << "31245非法" << endl;
    }
    return 0;
}
6.基本计算器
6.1 题目描述

给你一个字符串表达式 s ,请你实现一个基本计算器来计算并返回它的值。

1 <= s.length <= 3 * 105
s 由数字、'+'、'-'、'('、')'、和 ' ' 组成
s 表示一个有效的表达式
6.2 解题思路

在这里插入图片描述

图1:栈内元素计算

在这里插入图片描述

图2:使用栈处理优先级

在这里插入图片描述

图3:将字符串转化为数字

在这里插入图片描述

图4:计算函数

在这里插入图片描述

图5:字符串处理思路

7.数组中的第K个最大元素
7.1 题目描述

在未排序的数组中找到第 k 个最大的元素。请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素。

7.2 解题思路

在这里插入图片描述

图1:思路
在这里插入图片描述
图2:思路

7.3 C++实现
class Solution {
public:
    int findKthLargest(vector<int>& nums, int k) {
        priority_queue<int,vector<int>,greater<int>> Q;
        for(int i=0;i<nums.size();i++){
            if(Q.size()<k){
                Q.push(nums[i]);
            }
            else if(Q.top()<nums[i]){
                Q.pop();
                Q.push(nums[i]);
            }
        }
        return Q.top();
    }
};
8.数据流的中位数
8.1 题目描述

中位数是有序列表中间的数。如果列表长度是偶数,中位数则是中间两个数的平均值。

例如,
[2,3,4] 的中位数是 3
[2,3] 的中位数是 (2 + 3) / 2 = 2.5

设计一个支持以下两种操作的数据结构:
void addNum(int num) - 从数据流中添加一个整数到数据结构中。
double findMedian() - 返回目前所有元素的中位数。

8.2 解题思路

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

8.3 C++实现
class MedianFinder {
public:
    /** initialize your data structure here. */
    MedianFinder() {

    }
    
    void addNum(int num) {
        if(big_queue.empty()){
            big_queue.push(num);
            return;
        }
        if(big_queue.size()==small_queue.size()){
            if(num<big_queue.top()){
                big_queue.push(num);
            }
            else{
                small_queue.push(num);
            }
        }
        else if(big_queue.size()>small_queue.size()){
            if(num>big_queue.top()){
                small_queue.push(num);
            }
            else{
                small_queue.push(big_queue.top());
                big_queue.pop();
                big_queue.push(num);
            }
        }
        else if(small_queue.size()>big_queue.size()){
            if(num<small_queue.top()){
                big_queue.push(num);
            }
            else{
                big_queue.push(small_queue.top());
                small_queue.pop();
                small_queue.push(num);
            }
        }
    }
    
    double findMedian() {
        if(small_queue.size()==big_queue.size()){
            return (small_queue.top()+big_queue.top())/2;
        }
        else if(small_queue.size()>big_queue.size()){
            return small_queue.top();
        }
        return big_queue.top();
        
    }
private:
    priority_queue<double,vector<double>,greater<double>> small_queue;
    priority_queue<double,vector<double>,less<double>> big_queue;
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值