您的位置:首页 > 编程语言 > C语言/C++

C++ LeetCode 栈 队列 堆 专题

2020-04-27 07:18 766 查看

文章目录

  • LeetCode 225. Implement Stack using Queues
  • LeetCode 232. Implement Queue using Stacks
  • LeetCode 155. Min Stack
  • poj 1363 Rails
  • LeetCode 215. Kth Largest Element in an Array
  • 前提知识点

    1. stack

    栈,先进后出的线性表,可理解为一口水井。
    常用函数
    S.top() 取出栈顶
    S.empty() 判断栈是否为空
    S.push(x) 将x添加至栈
    S.pop() 弹出栈顶
    S.size() 栈的存储元素个数

    2. queue

    队列,先进先出的线性表,可理解为一根水平管道。
    常用函数
    Q.empty() 判断队列是否为空
    Q.front() 返回队列头部元素
    Q.back() 返回队列尾部元素
    Q.pop() 弹出队列头部元素
    Q.push(x) 将x添加至队列尾部
    Q.size() 返回队列存储元素的个数

    3.priority_queue

    STL优先级队列(二叉堆),最大(最小)值先出的完全二叉树。

    #include<queue>
    
    std::priority_queue<int>big_heap;//默认构造最大堆
    std::priority_queue<int,std::vector<int>,
    std::greater<int> >small_heap;//最小堆构造方法
    std::priority_queue<int,std::vector<int>,
    std::less<int> >big_heap2;//最大堆构造方法

    常用函数
    big_heap.empty() 判断堆是否为空
    big_heap.pop() 弹出堆顶元素(最大值)
    big_heap.push(x) 将元素x添加至二叉堆
    big_heap.top() 返回堆顶元素(最大值)
    big_heap.size() 返回堆中元素个数

    LeetCode 225. Implement Stack using Queues

    难度:Easy
    使用临时队列,注意题目中queue为复数!

    class MyStack {
    public:
    /** Initialize your data structure here. */
    MyStack() {
    
    }
    
    /** Push element x onto stack. */
    void push(int x) {
    std::queue<int>temp_queue;
    temp_queue.push(x);
    while(!data_queue.empty()){
    temp_queue.push(data_queue.front());
    data_queue.pop();
    }
    while(!temp_queue.empty()){
    data_queue.push(temp_queue.front());
    temp_queue.pop();
    }
    }
    
    /** Removes the element on top of the stack and returns that element. */
    int pop() {
    int a=data_queue.front();
    data_queue.pop();
    return a;
    }
    
    /** Get the top element. */
    int top() {
    return data_queue.front();
    }
    
    /** Returns whether the stack is empty. */
    bool empty() {
    return data_queue.empty();
    }
    private:
    std::queue<int>data_queue;
    };

    LeetCode 232. Implement Queue using Stacks

    难度:Easy
    方法一:使用双栈,题目中stack为复数!

    class MyQueue {
    public:
    /** Initialize your data structure here. */
    MyQueue() {
    
    }
    
    /** Push element x to the back of queue. */
    void push(int x) {
    data_stack1.push(x);
    }
    
    /** Removes the element from in front of queue and returns that element. */
    int pop() {
    if(data_stack2.empty()){
    while(!data_stack1.empty()){
    data_stack2.push(data_stack1.top());
    data_stack1.pop();
    }
    }
    int a=data_stack2.top();
    data_stack2.pop();
    return a;
    }
    
    /** Get the front element. */
    int peek() {
    if(data_stack2.empty()){
    while(!data_stack1.empty()){
    data_stack2.push(data_stack1.top());
    data_stack1.pop();
    }
    }
    return data_stack2.top();
    }
    
    /** Returns whether the queue is empty. */
    bool empty() {
    return data_stack1.empty()&&data_stack2.empty();
    }
    private:
    std::stack<int>data_stack1;
    std::stack<int>data_stack2;
    };

    方法二:使用临时栈

    class MyQueue {
    public:
    /** Initialize your data structure here. */
    MyQueue() {
    
    }
    
    /** Push element x to the back of queue. */
    void push(int x) {
    std::stack<int>temp_stack;
    while(!data_stack1.empty()){
    temp_stack.push(data_stack1.top());
    data_stack1.pop();
    }
    temp_stack.push(x);
    while(!temp_stack.empty()){
    data_stack1.push(temp_stack.top());
    temp_stack.pop();
    }
    }
    
    /** Removes the element from in front of queue and returns that element. */
    int pop() {
    int a=data_stack1.top();
    data_stack1.pop();
    return a;
    }
    
    /** Get the front element. */
    int peek() {
    return data_stack1.top();
    }
    
    /** Returns whether the queue is empty. */
    bool empty() {
    return data_stack1.empty();
    }
    private:
    std::stack<int>data_stack1;
    };

    LeetCode 155. Min Stack

    难度:Easy

    class MinStack {
    public:
    /** initialize your data structure here. */
    MinStack() {
    
    }
    
    void push(int x) {
    if(data_stack.empty()){
    data_stack.push(x);
    min_stack.push(x);
    }
    else{
    if(x<min_stack.top()){
    min_stack.push(x);
    }
    else{
    min_stack.push(min_stack.top());
    }
    data_stack.push(x);
    }
    }
    
    void pop() {
    data_stack.pop();
    min_stack.pop();
    }
    
    int top() {
    return data_stack.top();
    }
    
    int getMin() {
    return min_stack.top();
    }
    private:
    std::stack<int>data_stack;
    std::stack<int>min_stack;
    };

    poj 1363 Rails

    难度:Medium

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

    LeetCode 215. Kth Largest Element in an Array

    难度:Easy
    方法一:

    class Solution {
    public:
    int findKthLargest(vector<int>& nums, int k) {
    int size_nums=nums.size();
    std::sort(nums.begin(),nums.end());
    return nums[size_nums-k];
    }
    };

    方法二:

    class Solution {
    public:
    int findKthLargest(vector<int>& nums, int k) {
    std::priority_queue<int>big_heap;
    for(int i=0;i<nums.size();i++){
    big_heap.push(nums[i]);
    }
    for(int i=0;i<k-1;i++){
    big_heap.pop();
    }
    return big_heap.top();
    }
    };

    方法三:最优解

    class Solution {
    public:
    int findKthLargest(vector<int>& nums, int k) {
    std::priority_queue<int, std::vector<int>, std::greater<int> >small_heap;
    for(int i=0;i<nums.size();i++){
    if(small_heap.size()<k){
    small_heap.push(nums[i]);
    }
    else {
    if(nums[i]>small_heap.top()){
    small_heap.pop();
    small_heap.push(nums[i]);
    }
    }
    }
    return small_heap.top();
    }
    };
    • 点赞
    • 收藏
    • 分享
    • 文章举报
    Gotclue 发布了5 篇原创文章 · 获赞 0 · 访问量 143 私信 关注
    内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
    标签: