【剑指offer】包含min函数的栈
生活随笔
收集整理的這篇文章主要介紹了
【剑指offer】包含min函数的栈
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
題目:定義棧的數(shù)據(jù)結(jié)構(gòu),請(qǐng)?jiān)谠擃愋椭袑?shí)現(xiàn)一個(gè)能夠得到棧最小元素的min函數(shù)。
思路:簡(jiǎn)單。
注意:在遍歷棧的過程中,用到迭代器,Iterator中的next,指針發(fā)生了移動(dòng)
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;}}?
轉(zhuǎn)載于:https://www.cnblogs.com/lfdingye/p/7298663.html
總結(jié)
以上是生活随笔為你收集整理的【剑指offer】包含min函数的栈的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux Shell 简介
- 下一篇: 小班游戏教案反思《圈圈乐》