java數(shù)據(jù)結(jié)構(gòu)之棧的詳解
一、棧
棧的特性就是先進(jìn)后出,常用方法是入棧(push()),出棧(pop()),??眨╡mpty()),看棧頂元素(peek());
1.棧的應(yīng)用
1.1括號(hào)匹配
public boolean isValid(String s) { //有效括號(hào)時(shí)隔4個(gè)月后重新打卡 看看棧學(xué)的怎么樣 Stack<Character> stack=new Stack<>(); for(int i=0;i<s.length();i++){ char ch=s.charAt(i); if(ch=='('||ch=='{'||ch=='['){ stack.push(ch); }else{ if(stack.empty()){ //右括號(hào)多 return false; }else{ char ch1=stack.peek(); if(ch1=='{'&&ch=='}'||ch1=='['&&ch==']'||ch1=='('&&ch==')'){ stack.pop(); }else{ return false; } } } } if(!stack.empty()){ return false; } return true; }
1.2后綴表達(dá)式
a+b 這是我們最常見的表達(dá)式
前綴表達(dá)式就是+ab
后綴表達(dá)式就是ab+
轉(zhuǎn)換方式就是每一個(gè)表達(dá)式用括號(hào)括起,將兩個(gè)表達(dá)式中間的運(yùn)算符放到括號(hào)外,加括號(hào)的順序就是先乘除后加減
逆波蘭表達(dá)式求值:這里是后綴表達(dá)式,所以減法就是后出的減先出的,除法也是。利用棧的特性來(lái)實(shí)現(xiàn)后綴表達(dá)式
public int evalRPN(String[] tokens) { Stack <Integer> stack=new Stack<>(); int num1=0; int num2=0; for(String str:tokens){ if(str.equals("+")){ num1=stack.pop(); num2=stack.pop(); stack.push(num1+num2); }else if(str.equals("-")){ num1=stack.pop(); num2=stack.pop(); stack.push(num2-num1); }else if(str.equals("*")){ num1=stack.pop(); num2=stack.pop(); stack.push(num1*num2); }else if(str.equals("/")){ num1=stack.pop(); num2=stack.pop(); stack.push(num2/num1); }else{ stack.push(Integer.parseInt(str)); } } return stack.pop(); }
1.3用棧實(shí)現(xiàn)隊(duì)列
用棧模擬出隊(duì)列的push(),pop(),peek(),empty() 方法
class MyQueue { public Stack<Integer> stack1; public Stack<Integer> stack2; /** Initialize your data structure here. */ public MyQueue() { stack1 =new Stack<>(); stack2 =new Stack<>(); } /** Push element x to the back of queue. */ public void push(int x) { stack1.push(x); } /** Removes the element from in front of queue and returns that element. */ public int pop() { if(stack2.empty()){ while(!stack1.empty()){ stack2.push(stack1.pop()); } } return stack2.pop(); } /** Get the front element. */ public int peek() { if(stack2.empty()){ while(!stack1.empty()){ stack2.push(stack1.pop()); } } return stack2.peek(); } /** Returns whether the queue is empty. */ public boolean empty() { return stack1.empty()&&stack2.empty(); } } /** * Your MyQueue object will be instantiated and called as such: * MyQueue obj = new MyQueue(); * obj.push(x); * int param_2 = obj.pop(); * int param_3 = obj.peek(); * boolean param_4 = obj.empty(); */
1.4最小棧
class MinStack { //定義雙棧來(lái)實(shí)現(xiàn)最小棧 public Deque<Integer> stack1; public Deque<Integer> minStack; /** initialize your data structure here. */ public MinStack() { stack1=new LinkedList<Integer>(); minStack=new LinkedList<Integer>(); minStack.push(Integer.MAX_VALUE); } public void push(int val) { stack1.push(val); minStack.push(Math.min(val,minStack.peek())); } public void pop() { stack1.pop(); minStack.pop(); } public int top() { return stack1.peek(); } public int getMin() { return minStack.peek(); } } /** * Your MinStack object will be instantiated and called as such: * MinStack obj = new MinStack(); * obj.push(val); * obj.pop(); * int param_3 = obj.top(); * int param_4 = obj.getMin(); */
1.5棧的壓入和彈出序列
先看題目要求:輸入兩個(gè)整數(shù)序列,第一個(gè)序列表示棧的壓入順序,第二個(gè)序列表示棧的彈出序列,請(qǐng)判斷是否為合法的出棧序列
public boolean validateStackSequences(int []pushed,int []popped){ Stack <Integer> stack=new Stack<>(); int i=0; for(int num:pushed){ stack.push(num); while(!stack.isEmpty()&&stack.peek()==popped[i]){ i++; stack.pop(); } } return stack.isEmpty(); }
總結(jié)
本篇文章就到這里了,希望能給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
eclipse中maven的pom.xml文件中增加依賴的方法
日 在Maven項(xiàng)目中,可以使用pom.xml文件來(lái)添加依賴包,本文主要介紹了eclipse中maven的pom.xml文件中增加依賴的方法,具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12利用idea快速搭建一個(gè)spring-cloud(圖文)
本文主要介紹了idea快速搭建一個(gè)spring-cloud,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07Java Stopwatch類,性能與時(shí)間計(jì)時(shí)器案例詳解
這篇文章主要介紹了Java Stopwatch類,性能與時(shí)間計(jì)時(shí)器案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09Java OpenSSL生成的RSA公私鑰進(jìn)行數(shù)據(jù)加解密詳細(xì)介紹
這篇文章主要介紹了Java OpenSSL生成的RSA公私鑰進(jìn)行數(shù)據(jù)加解密詳細(xì)介紹的相關(guān)資料,這里提供實(shí)例代碼及說(shuō)明具體如何實(shí)現(xiàn),需要的朋友可以參考下2016-12-12Easyui的combobox實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)級(jí)聯(lián)效果
這篇文章主要介紹了Easyui的combobox實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)級(jí)聯(lián)效果的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-06-06Java十大經(jīng)典排序算法的實(shí)現(xiàn)圖解
Java常見的排序算法有:插入排序、希爾排序、選擇排序、冒泡排序、歸并排序、快速排序、堆排序、基數(shù)排序等。本文詳解介紹Java十大十大經(jīng)典排序算法的實(shí)現(xiàn)以及圖解,需要的可以參考一下2022-03-03java利用Apache commons codec進(jìn)行MD5加密,BASE64加密解密,執(zhí)行系統(tǒng)命令
這篇文章主要介紹了java利用apache Commons包進(jìn)行MD5加密,BASE64加密解密與執(zhí)行系統(tǒng)命令希望對(duì)大家有用2017-12-12不調(diào)用方法實(shí)現(xiàn)hutool導(dǎo)出excel圖片示例詳解
這篇文章主要為大家介紹了不調(diào)用方法實(shí)現(xiàn)hutool導(dǎo)出excel圖片示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08