当前位置: 首页 > ai >正文

数据结构:栈

栈:

栈的定义:

一种特殊的线性表,其只允许从其中一端进行删除和插入数据的操作,进行数据插入和删除的操作的一端叫做栈顶,另一端称为栈底,栈中的元素都遵循先出后进的原则

压栈:数据的插入操作叫做进栈/压栈/入栈,插入的数据在栈底。

出栈:数据的删除操作叫做出栈,出栈的数据在栈顶。

 栈的使用:

栈的模拟实现: 

知道了栈的功能后,我们模拟实现这些功能:

public class Mystack {int[] elem;//顶一个数组int usesize;//用来求栈内的有效元素的长度public Mystack(){//构造方法elem=new int[10];}private boolean isFull(){return elem.length==usesize;}public int push(int data){//入栈的方法if(isFull()){elem= Arrays.copyOf(elem,2*elem.length);}else{elem[usesize++]=data;}return data;}public int pop(){//出栈的方法if(Empty()){throw new EmptyException("栈内元素为空");//抛一个异常}int tem=elem[usesize-1];usesize--;return tem;}public int peek(){//获得栈顶的方法if(Empty()){throw new EmptyException("栈内元素为空");//抛一个异常}return elem[usesize-1];}public int size(){//求栈内有效元素的个数return usesize;}public boolean Empty(){//判断栈内是否为空return usesize==0;}
}

栈的应用场景:

问题1:括号匹配:

20. 有效的括号 - 力扣(LeetCode)

要求:

 这个题,我们用栈来解决:

public boolean isValid(String s) {Stack<Character> stack=new Stack<Character>();for(int i=0;i<s.length();i++){//遍历字符串char ch=s.charAt(i);//拿到每单个括号if(ch=='(' || ch=='[' || ch=='{'){//判断是不是左括号stack.push(ch);}else{//不是左括号的情况if(stack.isEmpty()){//栈为空,但字符串没有遍历完return false;}if(ch==')'&&stack.peek()=='('||ch==']'&&stack.peek()=='['||ch=='}'&&stack.peek()=='{'){//判断左右括号是否匹配stack.pop();}else{//括号不匹配return false;}}}if(!stack.isEmpty()){//字符串遍历完了,但是栈中还有括号return false;}return true;}

 问题2:逆波兰表达式求值:

150. 逆波兰表达式求值 - 力扣(LeetCode)

 public int evalRPN(String[] tokens) {Stack<Integer> stack=new Stack<Integer>();for(String st : tokens){//开始遍历字符串if(!isoperter(st)){//如果不为运算符int x=Integer.parseInt(st);stack.push(x);}else{//遍历到数字字符时int x1=stack.pop();int x2=stack.pop();switch(st){case "+":stack.push(x2+x1);break;case "-":stack.push(x2-x1);break;case "*":stack.push(x2*x1);break;case "/":stack.push(x2/x1);break;}}}return stack.peek();//最后运算结果}private boolean isoperter(String ch){//判断字符是否是运算字符if(ch.equals("+")||ch.equals("-")||ch.equals("*")||ch.equals("/")){return true;}return false;    }

问题3:栈的压入,弹出序列:

 public boolean IsPopOrder (int[] pushV, int[] popV) {Stack<Integer> stack=new Stack<Integer>();int j=0;for(int i=0;i<pushV.length;i++){//遍历popVstack.push(pushV[i]);//压栈while(!stack.isEmpty()&&j<popV.length&&stack.peek()==popV[j]){//判断栈顶元素和popV[j]是否相同stack.pop();j++;}}return stack.isEmpty();  //栈是否为空,作为返回结果}

 问题4:最小栈

155. 最小栈 - 力扣(LeetCode)

class MinStack {
Stack<Integer> stack;
Stack<Integer> minstack;public MinStack() {stack=new Stack<>();minstack =new Stack<>();}public void push(int val) {stack.push(val);if(minstack.isEmpty()){minstack.push(val);}else{if(val<=minstack.peek()){minstack.push(val);}}}public void pop() {if(stack.isEmpty()){return ;}int ret=stack.pop();if(minstack.isEmpty()){return ;}if(ret==minstack.peek()){minstack.pop();}}public int top() {if(stack.isEmpty()){return -1;}return stack.peek();}public int getMin() {if(minstack.isEmpty()){return -1;}return minstack.peek();}
}

http://www.xdnf.cn/news/1250.html

相关文章:

  • Multi-View Stereo for Community Photo Collections
  • 云原生--CNCF-1-云原生计算基金会介绍(云原生生态的发展目标和未来)
  • C语言学习记录(17)编译和链接
  • 硬件工程师面试常见问题(5)
  • C语言教程(十一):C 语言中四种主要作用域及作用域嵌套遮蔽
  • 2023蓝帽杯初赛内存取证-8
  • 【Dart语言】八、并发
  • 宏函数 和 C++ 内联函数
  • java知识点
  • Swoole-添加自定义路由实现控制器访问
  • 互联网三高-高性能之IO网络技术底层机制
  • 《TCP/IP详解 卷1:协议》之第四、五章:ARP RARP
  • CLIP | 训练过程中图像特征和文本特征的在嵌入空间中的对齐(两个投影矩阵的学习)
  • 武装Burp Suite工具:RouteVulScan插件_被动扫描发现漏洞.
  • python高级特性01
  • 代码随想录算法训练营Day34
  • (16)VTK C++开发示例 --- 转换文件格式
  • ProxySQL 性能调优工具推荐
  • day48—双指针-通过删除字母匹配到字典最长单词(LeetCode-524)
  • 【随机过程】柯尔莫哥洛夫微分方程总结
  • 算力网络的早期有关论文——自用笔记
  • 在Pytorch中使用Tensorboard可视化训练过程
  • 基于DeepSeek的网络爬虫技术创新与实践应用
  • 若依如何切换 tab 不刷新
  • Google 开发者政策中心 - 3 月版
  • 【Spring】AutoConfigureOrder与Order注解的区别与使用方式
  • Gboard安卓版手势输入与多语言支持全面评测【输入顺滑】
  • Java数组
  • C++抽象基类定义与使用
  • linux kallsys