劍指offer
兩道關於 資料結構——棧 的題目
1. 包含min函式的棧
簡要分析一下這道題,這道題做了3遍才過,踩了一些小坑
看看示例:
得到了規律,那麼關鍵部分的**實現,就在於 兩個棧(rawstack 和 minstack) 和 push() 方法
stackrawstack = new stack<>();其他部分就比較簡單stack
minstack = new stack<>();
public
void push1(int
node)
}minstack.push(min);
}
public2. 棧的壓入、彈出序列void
pop()
public
inttop()
public
intmin()
同樣,事先分析題目
如上分析:
感覺這題還是在於 抽象思維 轉化為 **,還有注意特殊情況,這題也是測了3遍過的,實現**比較簡單。
public兩題的具體原始碼可以在 裡的offer包中看看static
boolean ispoporder(int pusha, int
popa)
//獲取對應的下一個元素所在下標
int index = pushseq.indexof(popseq.get(0));
popseq.remove(0);
int nextindex = pushseq.indexof(popseq.get(0));
if (math.abs(nextindex - index) == 1 || nextindex == pushseq.size() - 1)
pushseq.remove(index);
else
return
false
; }
return
true
; }