【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 minimum element in the stack.

 1 class MinStack {
 2 private:
 3     stack<int> stk;
 4     stack<int> minstack;
 5 public:
 6     void push(int x) {
 7         stk.push(x);
 8         if(minstack.empty())
 9         {
10             minstack.push(x);
11         }else
12         {
13             if(x<=minstack.top())
14                 minstack.push(x);
15         }
16     }
17
18     void pop() {
19         if(stk.top()==minstack.top())
20         {
21             stk.pop();
22             minstack.pop();
23         }else
24         {
25             stk.pop();
26         }
27     }
28
29     int top() {
30         return stk.top();
31     }
32
33     int getMin() {
34         return minstack.top();
35     }
36 };
时间: 2024-12-15 06:16:04

【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】Min Stack -- python版

题目描述: 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

【LeetCode】栈 stack(共40题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [20]Valid Parentheses [42]Trapping Rain Water [71]Simplify Path [84]Largest Rectangle in Histogram [85]Maximal Rectangle [94]Binary Tree Inorder Traversal [103]Binary Tree Zigzag Level

【LeetCode】Implement Stack using Queues 解题报告

[题目] Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. empty() -- Return whether the stack is empty. Notes: You must use o

【LeetCode】设计题 design(共38题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [146]LRU Cache [155]Min Stack [170]Two Sum III - Data structure design [173]Binary Search Tree Iterator [208]Implement Trie (Prefix Tree) [211]Add and Search Word - Data structure desig

【Leetcode】Minimum Path Sum

Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. 思路:简单的动态规划题目,设f(m, n)为从(0, 0)到达(m

【LeetCode】Simplify Path

Simplify Path Given an absolute path for a file (Unix-style), simplify it. Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c"

【Leetcode】Binary Tree Postorder Traversal

Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 思路:后序遍历比起先序遍历以及中序遍历要稍微复杂一点,可以考虑用两个stack进行操作,

【leetcode】com/problems/surrounded-regions/

dfs 栈溢出,bfs超时,用dfs非递归就不溢出了,前后写了1一个星期class node { int i; int j; public node(int i1,int j1) { i=i1; j=j1; } } public class Solution { public void solve(char[][] board) { int len1=board.length; if(len1<=0)return; int len2=board[0].length; if(len1<=2||l