转自:http://www.programcreek.com/2014/02/leetcode-min-stack-java/
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.
Thoughts
An array is a perfect fit for this problem. We can use a integer to track the top of the stack. You can use the Stack class from Java SDK, but I think a simple array is more efficient and more beautiful.
Java Solution
class MinStack { private int[] arr = new int[100]; private int index = -1; public void push(int x) { if(index == arr.length - 1){ arr = Arrays.copyOf(arr, arr.length*2); } arr[++index] = x; } public void pop() { if(index>-1){ if(index == arr.length/2 && arr.length > 100){ arr = Arrays.copyOf(arr, arr.length/2); } index--; } } public int top() { if(index > -1){ return arr[index]; }else{ return 0; } } public int getMin() { int min = Integer.MAX_VALUE; for(int i=0; i<=index; i++){ if(arr[i] < min) min = arr[i]; } return min; } } |
相关推荐
MinStack minStack = new MinStack(); minStack.push(-2); minStack.push(0); minStack.push(-3); minStack.min(); --> 返回 -3. minStack.pop(); minStack.top(); --> 返回 0. minStack.min(); --> 返回 -...
leetcode 分类 Leetcode代码题解 随缘刷题,选择性更新题解 LeetCode 94 144 145 二叉树的前中后序遍历: Leetcode ...longest-consecutive-sequence ...Min Stack 最小栈: LeetCode 773 滑动谜题: ......
* [Stack](https://github.com/kamyu104/LeetCode#stack) * [Queue](https://github.com/kamyu104/LeetCode#queue) * [Heap](https://github.com/kamyu104/LeetCode#heap) * [Tree]...
leetcode题库 JavaScript / TypeScript for LeetCode 谨以此仓库记录LeetCode刷题历程【2020.6.27 -> undefined】 使用JavaScript、TypeScript语言解决数据结构和算法问题 锻炼编程能力,培养算法思维,相互学习,...
设计包含最小函数min(),取出元素函数pop(),加入元素函数push()的栈MinStack,实现其中指定的方法(2)。 MinStack中数据存储使用Java原生的Stack,存储数据元素为int。请实现以下对应的方法,完善功能。 ...
在任何给定的时间点,minStack 将保持最小元素直到现在(推送或弹出值) 满足条件的线性搜索或二分搜索,因为解决方案始终存在 由于其他方法为大输入提供 TLE,因此筛选埃托色尼 按位解决方案,因为二的幂只包含 1 ...
155.Min Stack 设置一个辅助栈专门用于放置当前最小的值,每次压栈时主栈压入新数据,辅助栈压入当前最小数据 316.Remove 重要 单调栈的问题,栈内元素大致递增,记录下串内字符的数量。有四题大致相同,总结一下...
Min Stack 完成LeetCode 232. Implement Queue using Stacks Week5 完成LeetCode 147. Insertion Sort List 完成作业Quick sort Week6 10/11 国庆弹性放假 Week7 完成作业Heap sort Week8 完成作业Merge sort Week9 ...
minStack stack 111 二叉树的最小深度 , tree 110 平衡二叉树 tree 104 二叉树的最大深度 tree 100 相同的树 tree 94 二叉树的中序遍历 tree 70 爬楼梯 dp 53 最大子序和 dp 26 删除排序数组中的重复项 array 1 两数...
包含min函数的栈(The_stack_containing_the_min_function.java) 队列的最大值(The_maximum_value_of_the_queue.java) 堆(heap_items) 最小的k个数(The_smallest_k_number.java) 位运算(bit_...
INT_MAX/INT_MIN. 3 总和 Size of vector >= 3 Three pointers, one move from 0 to n-1, other two are the same as in Two Sum Check duplicates 有效的副词 Stack is not empty, all paratheses are left ...
(min stack), 3.5 (two stack queue) 重要。两道题面M被问到过。3.6 (sort stack)感觉也有可能被考到。 第四章 (4.1, 4.3, 4.5 Leetcode上有): 感觉4.2, 4.3, 4.5,4.6, 4.7 重要。4.5 (valid BST)面E,Q碰到...
Min Stack 数据结构为两个栈, 一个记录最小值的栈, 一个是存放正常压栈数据 入栈的时候判断新加入的数是否小于或等于最小值栈里的top 出栈时判断出栈数字和最小栈里的是否相等 Evaluate Reverse Polish Notation 逆...
Stack Easy 394 字符串编码 Decode String Medium 链表 No Problem Solution Difficulty Tag 2 两数相加 Add Two Numbers Medium 19 删除链表倒数第N个节点 Remove Nth Node From End of List Medium 21 合并两个...
leetcode 设计-1 问题1: 设计Hashmap () 问题2: 设计 MinStack ()
代码type MinStack struct {a []int//存储元素b []int //辅助栈 维护栈的最小值/* initialize your dat
├─min_stack 最小栈 (https://leetcode-cn.com/explore/learn/card/queue-stack/218/stack-last-in-first-out-data-structure/877/) │ │ ├─valid_parentheses 有效的括号 ...
min-stack design-circular-deque trapping-rain-water 滑动窗口 通过窗口的大小不断调整来找到合适的值,或者窗口大小不变,通过左右移动来找到相应的结果 \ \ 其他 非 LeetCode 题,单纯在别人面试中看到的 \ 链表...
lru缓存leetcode 力扣_30天 力扣 30 天挑战赛 日 问题描述 问题和解决方案链接 Git 解决方案页面 1 SINGLE NUMBER 2 HAPPY NUMBER 3 MAXIMUM SUBARRAY 4 Move Zeroes 5 Best Time to Buy and Sell Stock II 6 GROUP ...
leetcode分发糖果 Leetcode C++ Solution Don't try to understand ...155-最小栈:min-stack Tree 普通二叉树 94-二叉树中序遍历:inorder-traversal 100-相同的二叉树:same-tree 101-对称二叉树:symmetric-