移情别恋c++ ദ്ദി˶ー̀֊ー́ ) ——8.stackqueue

2024-08-27 14:12
文章标签 c++ stackqueue 移情别恋

本文主要是介绍移情别恋c++ ദ്ദി˶ー̀֊ー́ ) ——8.stackqueue,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1.用栈实现队列

. - 力扣(LeetCode)

思路

1.将一个栈当作输入栈,用于压入 push 传入的数据;另一个栈当作输出栈,用于 pop 和 peek 操作。

2.每次 pop 或 peek 时,若输出栈为空将输入栈的全部数据依次弹出并压入输出栈,这样输出栈从栈顶往栈底的顺序就是队列从队首往队尾的顺序。

class MyQueue {
private:stack<int> inStack, outStack;void in2out() {while (!inStack.empty()) {outStack.push(inStack.top());inStack.pop();}}public:MyQueue() {}void push(int x) {inStack.push(x);}int pop() {if (outStack.empty()) {in2out();}int x = outStack.top();outStack.pop();return x;}int peek() {if (outStack.empty()) {in2out();}return outStack.top();}bool empty() {return inStack.empty() && outStack.empty();}
};

2. 用队列实现栈

 

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

3.数组中的第k个最大元素 

 思路:

1.先取数组的前k个元素,使用向上调整算法建立小堆(a[0]为最小值)

2.再遍历剩余数组,如果元素大于a[0],则替代a[0]入堆并使用向下调整算法调整

3.返回a[0];

 

typedef struct heap
{int* a;int size;int capacity;
}HP;void swap(int* p1, int* p2)
{int t = 0;t = *p1;*p1 = *p2;*p2 = t;
}void heapinit(HP* php)
{assert(php);php->a = NULL;php->size = 0;php->capacity = 0;
}void adjustup(int* a, int child)         //向上调整法          //上方必须是堆
{int parent = (child-1)/2;while (child>0){if (a[parent]>=a[child])        //小堆为>=,大堆为<={swap(&a[parent],&a[child]);child = parent;parent = (child - 1)/2;}else{break;}}
}void adjustdown(int* a,int n, int parent)     //向下调整法,!!!!!!(仅适用于根结点两端都是大堆或小堆)
{int child = 2 * parent + 1;while (child<=n)                   //{if (child + 1<=n && a[child + 1] < a[child])      //   child+1>n可以推出child==n,所以只有左孩子{child++;                          //选出左右孩子中最大的一个‘>’,最小的为'<'}if (a[parent]>=a[child])        //大堆为“<=”,小堆为“>=”{swap(&a[parent], &a[child]);parent = child;child = 2 * parent + 1;     //每次都先找左孩子}else{break;}}
}void heappush(HP* php, int data)
{php->a[php->size] = data;php->size++;adjustup(php->a, php->size - 1);
}int findKthLargest(int* nums, int numsSize, int k) {HP sl;heapinit(&sl);sl.a=(int*)malloc(sizeof(int)*k);int i=0;for(i=0;i<k;i++)heappush(&sl, nums[i]);for(i=k;i<numsSize;i++){if(nums[i]>sl.a[0]){sl.a[0]=nums[i];adjustdown(sl.a,k-1,0);  //记得是k-1}}return sl.a[0];
}

4.最小栈 

 

class MinStack {
public:
public:void push(int x) {// 只要是压栈,先将元素保存到_elem中_elem.push(x);// 如果x小于_min中栈顶的元素,将x再压入_min中!!!!!!!!!!if (_min.empty() || x <= _min.top())_min.push(x);}void pop() {// 如果_min栈顶的元素等于出栈的元素,_min顶的元素要移除!!!!!!!!if (_min.top() == _elem.top())_min.pop();_elem.pop();}int top() { return _elem.top();}int getMin() { return _min.top(); }private:// 保存栈中的元素std::stack<int> _elem;// 保存栈的最小值std::stack<int> _min;
};

 

5.栈的弹出压入序列 

1.设置flag,i,i用来遍历pushv数组;

2.当arr.top()==popV[flag]时,arr.pop(),flag++,

3.当pushV[i]==popV[flag]时,flag++;

4.遍历完成后,

for(;flag<popV.size();flag++)

          {

            if(popV[flag]!=arr.top())

            return false;

            else

             {

                arr.pop();

             }

          }

5.return true

class Solution {
public:bool IsPopOrder(vector<int>& pushV, vector<int>& popV) {int flag=0;for(int i=0;i<pushV.size();i++){if(pushV[i]!=popV[flag]){if(!arr.empty()){if(arr.top()==popV[flag]){arr.pop();flag++;while(!(arr.empty())&&arr.top()==popV[flag]) 
//如果连续遇到arr.top()==popV[flag],则一直arr.pop();flag++,直至两者不同{arr.pop();flag++;}arr.push(pushV[i]);}else {arr.push(pushV[i]);}}elsearr.push(pushV[i]);}else{flag++;}}for(;flag<popV.size();flag++){if(popV[flag]!=arr.top())return false;else{arr.pop();}}return true;}private:stack<int> arr;
};

 

6.逆波兰表达式求值 

1.设置两个stack,一个存数字(arr),一个存符号(brr)

2.遍历数组,若为数字则 入栈arr;

3.若为符号,则入栈brr,并取brr.top,arr.top(两次)

进行operation,并把返回值压入arr栈中;

4.返回arr.top;

class Solution {
public:int operation(int a, int b, string s) {if (s[0] == '+')return b + a;        //记得是b在前,a在后,因为栈是后进先出if (s[0] == '-')return b - a;if (s[0] == '*')return b * a;if (s[0] == '/')return b / a;return 1; // 记得写一个return// 1,因为系统判定如果if都不走,那么就没有返回值}int evalRPN(vector<string>& tokens) {int num = 0;int result = 0;string j;int a;int b;int end;for (int i = 0; i < tokens.size(); i++) {if (tokens[i] == "+" || tokens[i] == "-" || tokens[i] == "*" ||tokens[i] == "/") {brr.push(tokens[i]);a = arr.top();arr.pop();b = arr.top();arr.pop();end = operation(a, b, brr.top());arr.push(end);brr.pop();} else {j = tokens[i];if (j[0] == '-') {for (int i = 1; i < j.size(); i++) {num = num * 10 + (j[i] - '0');}arr.push(-num);num = 0;} else {for (int i = 0; i < j.size(); i++) {num = num * 10 + (j[i] - '0');}arr.push(num);num = 0;}}}end = arr.top();return end;}private:stack<int> arr;stack<string> brr;
};

 

这篇关于移情别恋c++ ദ്ദി˶ー̀֊ー́ ) ——8.stackqueue的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1111894

相关文章

C++ move 的作用详解及陷阱最佳实践

《C++move的作用详解及陷阱最佳实践》文章详细介绍了C++中的`std::move`函数的作用,包括为什么需要它、它的本质、典型使用场景、以及一些常见陷阱和最佳实践,感兴趣的朋友跟随小编一起看... 目录C++ move 的作用详解一、一句话总结二、为什么需要 move?C++98/03 的痛点⚡C++

详解C++ 存储二进制数据容器的几种方法

《详解C++存储二进制数据容器的几种方法》本文主要介绍了详解C++存储二进制数据容器,包括std::vector、std::array、std::string、std::bitset和std::ve... 目录1.std::vector<uint8_t>(最常用)特点:适用场景:示例:2.std::arra

C++构造函数中explicit详解

《C++构造函数中explicit详解》explicit关键字用于修饰单参数构造函数或可以看作单参数的构造函数,阻止编译器进行隐式类型转换或拷贝初始化,本文就来介绍explicit的使用,感兴趣的可以... 目录1. 什么是explicit2. 隐式转换的问题3.explicit的使用示例基本用法多参数构造

C++,C#,Rust,Go,Java,Python,JavaScript的性能对比全面讲解

《C++,C#,Rust,Go,Java,Python,JavaScript的性能对比全面讲解》:本文主要介绍C++,C#,Rust,Go,Java,Python,JavaScript性能对比全面... 目录编程语言性能对比、核心优势与最佳使用场景性能对比表格C++C#RustGoJavapythonjav

C++打印 vector的几种方法小结

《C++打印vector的几种方法小结》本文介绍了C++中遍历vector的几种方法,包括使用迭代器、auto关键字、typedef、计数器以及C++11引入的范围基础循环,具有一定的参考价值,感兴... 目录1. 使用迭代器2. 使用 auto (C++11) / typedef / type alias

C++ scoped_ptr 和 unique_ptr对比分析

《C++scoped_ptr和unique_ptr对比分析》本文介绍了C++中的`scoped_ptr`和`unique_ptr`,详细比较了它们的特性、使用场景以及现代C++推荐的使用`uni... 目录1. scoped_ptr基本特性主要特点2. unique_ptr基本用法3. 主要区别对比4. u

C++11中的包装器实战案例

《C++11中的包装器实战案例》本文给大家介绍C++11中的包装器实战案例,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录引言1.std::function1.1.什么是std::function1.2.核心用法1.2.1.包装普通函数1.2.

C++多线程开发环境配置方法

《C++多线程开发环境配置方法》文章详细介绍了如何在Windows上安装MinGW-w64和VSCode,并配置环境变量和编译任务,使用VSCode创建一个C++多线程测试项目,并通过配置tasks.... 目录下载安装 MinGW-w64下载安装VS code创建测试项目配置编译任务创建 tasks.js

C++ 多态性实战之何时使用 virtual 和 override的问题解析

《C++多态性实战之何时使用virtual和override的问题解析》在面向对象编程中,多态是一个核心概念,很多开发者在遇到override编译错误时,不清楚是否需要将基类函数声明为virt... 目录C++ 多态性实战:何时使用 virtual 和 override?引言问题场景判断是否需要多态的三个关

C++简单日志系统实现代码示例

《C++简单日志系统实现代码示例》日志系统是成熟软件中的一个重要组成部分,其记录软件的使用和运行行为,方便事后进行故障分析、数据统计等,:本文主要介绍C++简单日志系统实现的相关资料,文中通过代码... 目录前言Util.hppLevel.hppLogMsg.hppFormat.hppSink.hppBuf