【剑指offer】包含min函数的栈
生活随笔
收集整理的這篇文章主要介紹了
【剑指offer】包含min函数的栈
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目:定義棧的數據結構,請在該類型中實現一個能夠得到棧最小元素的min函數。
思路:簡單。
注意:在遍歷棧的過程中,用到迭代器,Iterator中的next,指針發生了移動
import java.util.Iterator; import java.util.Stack;public class MinStack {Stack<Integer> stack = new Stack<Integer>();public void push(int node) {stack.push(node);}public void pop() {stack.pop();}public int top() {return stack.peek();}public int min() {int min = stack.peek();Iterator<Integer> it = stack.iterator();while(it.hasNext()){int temp = it.next();if(min > temp){min = temp;}}return min;}}?
轉載于:https://www.cnblogs.com/lfdingye/p/7298663.html
總結
以上是生活随笔為你收集整理的【剑指offer】包含min函数的栈的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux Shell 简介
- 下一篇: POJ 1753 Flip Game 高