LeetCode: Min Stack 解题报告

Min Stack

My Submissions

Question

Solution

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.

  • push(x) -- Push element x onto stack.
  • pop() -- Removes the element on top of the stack.
  • top() -- Get the top element.
  • getMin() -- Retrieve the minimum element in the stack.

Show Tags

Have you met this question in a real interview? Yes No

Discuss

SOLUTION 1:

比较直观。用一个min stack专门存放最小值,如果有比它小 或是相等的(有多个平行的最小值都要单独存放,否则pop后会出问题),

则存放其到minstack.具体看代码:

 1 class MinStack {
 2     Stack<Integer> elements = new Stack<Integer>();
 3     Stack<Integer> minStack = new Stack<Integer>();
 4
 5     public void push(int x) {
 6         elements.push(x);
 7         if (minStack.isEmpty() || x <= minStack.peek()) {
 8             minStack.push(x);
 9         }
10     }
11
12     public void pop() {
13         if (elements.isEmpty()) {
14             return;
15         }
16
17         // 这个地方太蛋疼了,居然要用equals...
18         if (elements.peek().equals(minStack.peek())) {
19             minStack.pop();
20         }
21         elements.pop();
22     }
23
24     public int top() {
25         return elements.peek();
26     }
27
28     public int getMin() {
29         return minStack.peek();
30     }
31 }

GITHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/stack/MinStack.java

时间: 2024-10-14 21:57:25

LeetCode: Min Stack 解题报告的相关文章

【LeetCode】Min Stack 解题报告

[题目] Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. getMin() -- Retrieve the mini

LeetCode: Combination Sum 解题报告

Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Question Solution Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The

[LeetCode]LRU Cache, 解题报告

题目 Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.

【LeetCode】Subsets 解题报告

[题目] Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,3], a solution is: [ [3], [1], [2], [1,2,

[LeetCode] Min Stack Min Stack

Min Stack Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. getMin() -- Retrieve the

[LeetCode]Min Stack,解题报告

目录 目录 题目 思路1 AC代码 缺陷 思路2 AC代码 题目 Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) – Push element x onto stack. pop() – Removes the element on top of the stack. top() – Get the top element. getM

LeetCode ZigZag Conversion 解题报告

对输入字符串,做蛇形变化,然后按行输出. https://oj.leetcode.com/problems/zigzag-conversion/ 例如:The string "PAYPALISHIRING"  的蛇形变化如下: P        A           H        N A   P   L    S     I     I   G Y         I            R 最后要求输出的就是:"PAHNAPLSIIGYIR" Write

LeetCode——Min Stack

Description: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. getMin() -- Retrieve

[leetcode] Min Stack @ Python

原题地址:https://oj.leetcode.com/problems/min-stack/ 解题思路:开辟两个栈,一个栈是普通的栈,一个栈用来维护最小值的队列. 代码: class MinStack: # @param x, an integer def __init__(self): self.stack1 = [] self.stack2 = [] # @return an integer def push(self, x): self.stack1.append(x) if len(