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(); }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/65485.html
摘要:一種是利用去找同一層的兩個邊,不斷累加寄存。雙指針法的思想先找到左右兩邊的第一個峰值作為參照位,然后分別向后向前每一步增加該位與參照位在這一位的差值,加入,直到下一個峰值,再更新為新的參照位。 Problem Given n non-negative integers representing an elevation map where the width of each bar i...
摘要:遞歸法不說了,棧迭代的函數是利用的原理,從根節點到最底層的左子樹,依次入堆棧。然后將出的結點值存入數組,并對出的結點的右子樹用函數繼續迭代。 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...
摘要:當你被的時候,人家問,一定要說,當然可以啦所以,不用,怎么辦幸好,這是一道的題目,只要逐層遍歷,就可以了。所以,試一試用堆棧的做法吧記得的特點是后入先出哦 Problem Given a binary tree, return the preorder traversal of its nodes values. Example Given: 1 / 2 3 ...
摘要:首先,根據迭代器需要不斷返回下一個元素,確定用堆棧來做。堆棧初始化數據結構,要先從后向前向堆棧壓入中的元素。在調用之前,先要用判斷下一個是還是,并進行的操作對要展開并順序壓入對直接返回。 Problem Given a nested list of integers, implement an iterator to flatten it. Each element is either...
閱讀 1785·2023-04-26 00:47
閱讀 1543·2021-11-11 16:55
閱讀 2597·2021-09-27 14:04
閱讀 3548·2021-09-22 15:58
閱讀 3554·2021-07-26 23:38
閱讀 2129·2019-08-30 13:47
閱讀 1979·2019-08-30 13:15
閱讀 1142·2019-08-29 17:09