面試題30.包含min函數的棧(155)
原題
題解
方法一:輔助棧和數據棧同步(劍指offer)
class MinStack {
Stack< Integer> A, B;
public MinStack ( ) {
A = new Stack < > ( ) ;
B = new Stack < > ( ) ;
}
public void push ( int x) {
A. push ( x) ;
if ( B. empty ( ) || x <= B. peek ( ) ) {
B. push ( x) ;
} else {
B. push ( B. peek ( ) ) ;
}
}
public void pop ( ) {
if ( ! A. empty ( ) ) {
A. pop ( ) ;
B. pop ( ) ;
}
}
public int top ( ) {
return A. peek ( ) ;
}
public int min ( ) {
return B. peek ( ) ;
}
}
import java. util. Stack;
public class MinStack {
private Stack< Integer> data;
private Stack< Integer> helper;
public MinStack ( ) {
data = new Stack < > ( ) ;
helper = new Stack < > ( ) ;
}
public void push ( int x) {
data. add ( x) ;
if ( helper. isEmpty ( ) || helper. peek ( ) >= x) {
helper. add ( x) ;
} else {
helper. add ( helper. peek ( ) ) ;
}
}
public void pop ( ) {
if ( ! data. isEmpty ( ) ) {
helper. pop ( ) ;
data. pop ( ) ;
}
}
public int top ( ) {
if ( ! data. isEmpty ( ) ) {
return data. peek ( ) ;
}
throw new RuntimeException ( "棧中元素爲空,此操作非法" ) ;
}
public int getMin ( ) {
if ( ! helper. isEmpty ( ) ) {
return helper. peek ( ) ;
}
throw new RuntimeException ( "棧中元素爲空,此操作非法" ) ;
}
}
作者:liweiwei1419
鏈接:https: / / leetcode- cn. com/ problems/ min- stack/ solution/ shi- yong- fu- zhu- zhan- tong- bu- he- bu- tong- bu- python- /
來源:力扣(LeetCode)
著作權歸作者所有。商業轉載請聯繫作者獲得授權,非商業轉載請註明出處。
方法二:輔助棧和數據棧不同步
class MinStack {
Stack< Integer> A, B;
public MinStack ( ) {
A = new Stack < > ( ) ;
B = new Stack < > ( ) ;
}
public void push ( int x) {
A. push ( x) ;
if ( B. empty ( ) || x <= B. peek ( ) ) {
B. push ( x) ;
}
}
public void pop ( ) {
if ( A. peek ( ) . equals ( B. peek ( ) ) ) {
B. pop ( ) ;
}
if ( ! A. empty ( ) ) {
A. pop ( ) ;
}
}
public int top ( ) {
return A. peek ( ) ;
}
public int min ( ) {
return B. peek ( ) ;
}
}
import java. util. Stack;
public class MinStack {
private Stack< Integer> data;
private Stack< Integer> helper;
public MinStack ( ) {
data = new Stack < > ( ) ;
helper = new Stack < > ( ) ;
}
public void push ( int x) {
data. add ( x) ;
if ( helper. isEmpty ( ) || helper. peek ( ) >= x) {
helper. add ( x) ;
}
}
public void pop ( ) {
if ( ! data. isEmpty ( ) ) {
int top = data. pop ( ) ;
if ( top == helper. peek ( ) ) {
helper. pop ( ) ;
}
}
}
public int top ( ) {
if ( ! data. isEmpty ( ) ) {
return data. peek ( ) ;
}
throw new RuntimeException ( "棧中元素爲空,此操作非法" ) ;
}
public int getMin ( ) {
if ( ! helper. isEmpty ( ) ) {
return helper. peek ( ) ;
}
throw new RuntimeException ( "棧中元素爲空,此操作非法" ) ;
}
}
作者:liweiwei1419
鏈接:https: / / leetcode- cn. com/ problems/ min- stack/ solution/ shi- yong- fu- zhu- zhan- tong- bu- he- bu- tong- bu- python- /
來源:力扣(LeetCode)
著作權歸作者所有。商業轉載請聯繫作者獲得授權,非商業轉載請註明出處。
面試題31.棧的壓入、彈出序列(946.驗證棧序列)
題目
安安解法:代碼有些繁瑣
class Solution {
public boolean validateStackSequences ( int [ ] pushed, int [ ] popped) {
Stack< Integer> stack = new Stack < > ( ) ;
int i = 0 , j = 0 ;
while ( j < popped. length) {
if ( i < pushed. length) {
if ( pushed[ i] != popped[ j] ) {
if ( ! stack. empty ( ) && stack. peek ( ) . equals ( popped[ j] ) ) {
stack. pop ( ) ;
j++ ;
} else {
stack. push ( pushed[ i] ) ;
i++ ;
}
} else {
j++ ;
i++ ;
}
} else {
int topE = stack. peek ( ) ;
if ( ! stack. empty ( ) && topE == popped[ j] ) {
stack. pop ( ) ;
j++ ;
} else {
return false ;
}
}
}
return true ;
}
}
官解:輔助棧
class Solution {
public boolean validateStackSequences ( int [ ] pushed, int [ ] popped) {
int N = pushed. length;
Stack< Integer> stack = new Stack ( ) ;
int j = 0 ;
for ( int x: pushed) {
stack. push ( x) ;
while ( ! stack. isEmpty ( ) && j < N && stack. peek ( ) == popped[ j] ) {
stack. pop ( ) ;
j++ ;
}
}
return j == N;
}
}
作者:LeetCode
鏈接:https: / / leetcode- cn. com/ problems/ validate- stack- sequences/ solution/ yan- zheng- zhan- xu- lie- by- leetcode/
來源:力扣(LeetCode)
著作權歸作者所有。商業轉載請聯繫作者獲得授權,非商業轉載請註明出處。