Problem
Implement a stack with min() function, which will return the smallest number in the stack.
It should support push, pop and min operation all in O(1) cost.
Examplepush(1)
pop() // return 1
push(2)
push(3)
min() // return 2
push(1)
min() // return 1
min operation will never be called if there is no number in the stack.
注意在push()里的條件,minstack.peek() >= number,保證最小值在minstack中。
valueOf(String) returns a new java.lang.Integer, which is the object representative of the integer,
whereas parseInt(String) returns a primitive integer type int.
intValue is an instance method whereby parseInt is a static method.
Moreover, Integer.parseInt(s) can take primitive datatype as well.
class MinStack { Stackstack = new Stack<>(); Stack minstack = new Stack<>(); /** initialize your data structure here. */ public MinStack() { } public void push(int x) { stack.push(x); if (minstack.isEmpty() || minstack.peek() >= x) minstack.push(x); } public void pop() { if (!stack.isEmpty() && !minstack.isEmpty() && minstack.peek().equals(stack.peek())) minstack.pop(); stack.pop(); } public int top() { if (stack.isEmpty()) return 0; else return stack.peek(); } public int getMin() { if (minstack.isEmpty()) return 0; else return minstack.peek(); } }
class MinStack { /** initialize your data structure here. */ private Node head; private class Node { int val; int min; Node next; private Node(int min, int val, Node next) { this.val = val; this.min = min; this.next = next; } } public void push(int x) { if (head == null) { head = new Node(x, x, null); } else { head = new Node(Math.min(head.min, x), x, head); } } public void pop() { head = head.next; return; } public int top() { return head.val; } public int getMin() { return head.min; } } /** * Your MinStack object will be instantiated and called as such: * MinStack obj = new MinStack(); * obj.push(x); * obj.pop(); * int param_3 = obj.top(); * int param_4 = obj.getMin(); */
public void push(int x) { if (maxStack.isEmpty() || maxStack.peek() <= x) maxStack.push(x); stack.push(x); } public int pop() { if (maxStack.peek().equals(stack.peek())) maxStack.pop(); return stack.pop(); } public int max() { return maxStack.peek(); }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://m.specialneedsforspecialkids.com/yun/65485.html
摘要:一種是利用去找同一層的兩個(gè)邊,不斷累加寄存。雙指針?lè)ǖ乃枷胂日业阶笥覂蛇叺牡谝粋€(gè)峰值作為參照位,然后分別向后向前每一步增加該位與參照位在這一位的差值,加入,直到下一個(gè)峰值,再更新為新的參照位。 Problem Given n non-negative integers representing an elevation map where the width of each bar i...
摘要:遞歸法不說(shuō)了,棧迭代的函數(shù)是利用的原理,從根節(jié)點(diǎn)到最底層的左子樹(shù),依次入堆棧。然后將出的結(jié)點(diǎn)值存入數(shù)組,并對(duì)出的結(jié)點(diǎn)的右子樹(shù)用函數(shù)繼續(xù)迭代。 Problem Given a binary tree, return the inorder traversal of its nodes values. Example Given binary tree {1,#,2,3}, 1 ...
Problem Given a binary tree, return the zigzag level order traversal of its nodes values. (ie, from left to right, then right to left for the next level and alternate between). Example Given binary tr...
摘要:當(dāng)你被的時(shí)候,人家問(wèn),一定要說(shuō),當(dāng)然可以啦所以,不用,怎么辦幸好,這是一道的題目,只要逐層遍歷,就可以了。所以,試一試用堆棧的做法吧記得的特點(diǎn)是后入先出哦 Problem Given a binary tree, return the preorder traversal of its nodes values. Example Given: 1 / 2 3 ...
摘要:首先,根據(jù)迭代器需要不斷返回下一個(gè)元素,確定用堆棧來(lái)做。堆棧初始化數(shù)據(jù)結(jié)構(gòu),要先從后向前向堆棧壓入中的元素。在調(diào)用之前,先要用判斷下一個(gè)是還是,并進(jìn)行的操作對(duì)要展開(kāi)并順序壓入對(duì)直接返回。 Problem Given a nested list of integers, implement an iterator to flatten it. Each element is either...
閱讀 1801·2023-04-26 00:47
閱讀 1552·2021-11-11 16:55
閱讀 2622·2021-09-27 14:04
閱讀 3559·2021-09-22 15:58
閱讀 3560·2021-07-26 23:38
閱讀 2137·2019-08-30 13:47
閱讀 1987·2019-08-30 13:15
閱讀 1151·2019-08-29 17:09