移情别恋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++11 <chrono> 库特性

《从入门到精通C++11<chrono>库特性》chrono库是C++11中一个非常强大和实用的库,它为时间处理提供了丰富的功能和类型安全的接口,通过本文的介绍,我们了解了chrono库的基本概念... 目录一、引言1.1 为什么需要<chrono>库1.2<chrono>库的基本概念二、时间段(Durat

C++20管道运算符的实现示例

《C++20管道运算符的实现示例》本文简要介绍C++20管道运算符的使用与实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录标准库的管道运算符使用自己实现类似的管道运算符我们不打算介绍太多,因为它实际属于c++20最为重要的

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性:

c++中的set容器介绍及操作大全

《c++中的set容器介绍及操作大全》:本文主要介绍c++中的set容器介绍及操作大全,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录​​一、核心特性​​️ ​​二、基本操作​​​​1. 初始化与赋值​​​​2. 增删查操作​​​​3. 遍历方

解析C++11 static_assert及与Boost库的关联从入门到精通

《解析C++11static_assert及与Boost库的关联从入门到精通》static_assert是C++中强大的编译时验证工具,它能够在编译阶段拦截不符合预期的类型或值,增强代码的健壮性,通... 目录一、背景知识:传统断言方法的局限性1.1 assert宏1.2 #error指令1.3 第三方解决

C++11委托构造函数和继承构造函数的实现

《C++11委托构造函数和继承构造函数的实现》C++引入了委托构造函数和继承构造函数这两个重要的特性,本文主要介绍了C++11委托构造函数和继承构造函数的实现,具有一定的参考价值,感兴趣的可以了解一下... 目录引言一、委托构造函数1.1 委托构造函数的定义与作用1.2 委托构造函数的语法1.3 委托构造函

C++11作用域枚举(Scoped Enums)的实现示例

《C++11作用域枚举(ScopedEnums)的实现示例》枚举类型是一种非常实用的工具,C++11标准引入了作用域枚举,也称为强类型枚举,本文主要介绍了C++11作用域枚举(ScopedEnums... 目录一、引言二、传统枚举类型的局限性2.1 命名空间污染2.2 整型提升问题2.3 类型转换问题三、C

C++链表的虚拟头节点实现细节及注意事项

《C++链表的虚拟头节点实现细节及注意事项》虚拟头节点是链表操作中极为实用的设计技巧,它通过在链表真实头部前添加一个特殊节点,有效简化边界条件处理,:本文主要介绍C++链表的虚拟头节点实现细节及注... 目录C++链表虚拟头节点(Dummy Head)一、虚拟头节点的本质与核心作用1. 定义2. 核心价值二

C++ 检测文件大小和文件传输的方法示例详解

《C++检测文件大小和文件传输的方法示例详解》文章介绍了在C/C++中获取文件大小的三种方法,推荐使用stat()函数,并详细说明了如何设计一次性发送压缩包的结构体及传输流程,包含CRC校验和自动解... 目录检测文件的大小✅ 方法一:使用 stat() 函数(推荐)✅ 用法示例:✅ 方法二:使用 fsee

Windows下C++使用SQLitede的操作过程

《Windows下C++使用SQLitede的操作过程》本文介绍了Windows下C++使用SQLite的安装配置、CppSQLite库封装优势、核心功能(如数据库连接、事务管理)、跨平台支持及性能优... 目录Windows下C++使用SQLite1、安装2、代码示例CppSQLite:C++轻松操作SQ