java數(shù)據(jù)結(jié)構(gòu)之棧的詳解
一、棧
棧的特性就是先進(jìn)后出,常用方法是入棧(push()),出棧(pop()),??眨╡mpty()),看棧頂元素(peek());
1.棧的應(yīng)用
1.1括號匹配
public boolean isValid(String s) {
//有效括號時隔4個月后重新打卡 看看棧學(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()){
//右括號多
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)換方式就是每一個表達(dá)式用括號括起,將兩個表達(dá)式中間的運(yùn)算符放到括號外,加括號的順序就是先乘除后加減
逆波蘭表達(dá)式求值:這里是后綴表達(dá)式,所以減法就是后出的減先出的,除法也是。利用棧的特性來實(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 {
//定義雙棧來實(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棧的壓入和彈出序列
先看題目要求:輸入兩個整數(shù)序列,第一個序列表示棧的壓入順序,第二個序列表示棧的彈出序列,請判斷是否為合法的出棧序列
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é)
本篇文章就到這里了,希望能給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
java中map和對象互轉(zhuǎn)工具類的實(shí)現(xiàn)示例
這篇文章主要介紹了java中map和對象互轉(zhuǎn)工具類的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
淺談Java日志框架slf4j作用及其實(shí)現(xiàn)原理
日志記錄是應(yīng)用程序運(yùn)行中必不可少的一部分。這篇文章主要介紹了淺談Java日志框架slf4j作用及其實(shí)現(xiàn)原理,SLF4J是一個日志框架抽象層,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-03-03
Java?Excel?Poi字體顏色自定義設(shè)置代碼
最近項(xiàng)目使用POI按模板導(dǎo)出Excel,需要設(shè)置單元格的字體為紅色,下面這篇文章主要給大家介紹了關(guān)于Java?Excel?Poi字體顏色自定義設(shè)置的相關(guān)資料,需要的朋友可以參考下2024-01-01
Springboot 使用 JSR 303 對 Controller 控制層校驗(yàn)及 Service 服務(wù)層 AOP 校驗(yàn)
這篇文章主要介紹了Springboot 使用 JSR 303 對 Controller 控制層校驗(yàn)及 Service 服務(wù)層 AOP 校驗(yàn) 使用消息資源文件對消息國際化的相關(guān)知識,需要的朋友可以參考下2017-12-12
解決OpenFeign遠(yuǎn)程調(diào)用返回的對象總是null問題
OpenFeign在SpringCloud中用于遠(yuǎn)程調(diào)用,配置簡單,在使用Ribbon或Hystrix時,需要注意path參數(shù)必須以/開頭,否則回參會是null2024-11-11
淺談Spring Boot Web 應(yīng)用性能優(yōu)化
這篇文章主要介紹了淺談Spring Boot Web 應(yīng)用性能優(yōu)化,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07
SpringBoot打成war包在tomcat或wildfly下運(yùn)行的方法
這篇文章主要介紹了SpringBoot打成war包在tomcat或wildfly下運(yùn)行的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-11-11

