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

数据结构:栈

栈:

栈的定义:

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

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

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

 栈的使用:

栈的模拟实现: 

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

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();}
}

相关文章:

  • notepad++技巧:查找和替换:扩展 or 正则表达式
  • 《Android系统应用部署暗礁:OAT文件缺失引发的连锁崩溃与防御体系构建》
  • 数据库基础——事务
  • AES-128、AES-192、AES-256 简介
  • 缓存,内存,本地缓存等辨析
  • Spark-Streaming(1)
  • 【Git】Git的远程分支已删除,为何本地还能显示?
  • oracle将表字段逗号分隔的值进行拆分,并替换值
  • ​CTGCache ​CTG-Cache TeleDB
  • 【MySQL数据库】表的约束
  • 工程投标k值分析系统(需求和功能说明)
  • 使用Multipart Form-Data一次请求获取多张图片
  • 真我推出首款 AI 翻译耳机,支持 32 种语言翻译
  • 2.5 函数的拓展
  • LangGraph(二)——QuickStart样例中的第二步
  • C++ std::forward 详解
  • 【源码】【Java并发】【ThreadLocal】适合中学者体质的ThreadLocal源码阅读
  • 在 40 亿整数中捕获“恰好出现两次”的数字
  • 动态提示词(小模型)、RAG和提示词系统
  • 【CPP】固定大小内存池
  • 海南陵水一酒店保洁员调包住客港币,被判刑一年六个月
  • 举报人不服相关部门奖励“缺斤少两”,两地分别作出再认定
  • 依托空域优势,浦江镇将建设上海首个“低空融合飞行示范区”
  • 图忆|温州旅沪先贤的家国情怀
  • 五角大楼正在“全面崩溃”?白宫被指已在物色新国防部长
  • 日媒:日本公明党党首将访华,并携带石破茂亲笔信