实现一个最小栈的push(int x),pop(),top(),min()方法,要求都是常量时间
2014-12-05 14:49
525 查看
原题目:https://oj.leetcode.com/problems/min-stack/solution/
实现一个最小栈的push(int x),pop(),top(),min()方法,要求都是常量时间
此代码已经被leetcode接受,下面的分析结果是leetcode给出的:
Hints:
Consider space-time tradeoff. How would you keep track of the minimums using extra space?
Make sure to consider duplicate elements.
Use an extra stack to keep track of the current minimum value. During the push operation we choose the new element or the current minimum, whichever that is smaller to push onto the min stack.
If a new element is larger than the current minimum, we do not need to push it on to the min stack. When we perform the pop operation, check if the popped element is the same as the current minimum. If it is, pop it off the min stack too.
Cancel rating
1
2
3
4
5
Average Rating: 4.0 (293 votes)
实现一个最小栈的push(int x),pop(),top(),min()方法,要求都是常量时间
此代码已经被leetcode接受,下面的分析结果是leetcode给出的:
Hints:
Consider space-time tradeoff. How would you keep track of the minimums using extra space?
Make sure to consider duplicate elements.
O(n) runtime, O(n) space – Extra stack:
Use an extra stack to keep track of the current minimum value. During the push operation we choose the new element or the current minimum, whichever that is smaller to push onto the min stack.
O(n) runtime, O(n) space – Minor space optimization:
If a new element is larger than the current minimum, we do not need to push it on to the min stack. When we perform the pop operation, check if the popped element is the same as the current minimum. If it is, pop it off the min stack too.Cancel rating
1
2
3
4
5
Average Rating: 4.0 (293 votes)
/** * @Author jiangfq * */ package com.test; import java.util.Random; /** * @author jiangfq * */ public class MinStack { private static final int INIT_CAPACITY = 16; private static final float FACTOR = 0.75f; private int[] a = null; private int currIndex = 0; private int size = 0; private int min = Integer.MAX_VALUE; private int minIndex = -1; public MinStack() { a = new int[INIT_CAPACITY]; } public MinStack(int capacity) { if(capacity < 1 || capacity > (Integer.MAX_VALUE - 8)) { throw new IllegalArgumentException("illegal capacity value: " + capacity); } a = new int[capacity]; } public void push(int x) { if(size > (Integer.MAX_VALUE - 8)) { throw new RuntimeException("no more space to store value"); } if(size > 0 && (size + 1) >= a.length) { int newSize = (int)(size+size*FACTOR); if(newSize > (Integer.MAX_VALUE - 8)) { throw new RuntimeException("no more space to store value"); } int[] b = new int[newSize]; for(int i = 0; i < size; i++) { b[i] = a[i]; } a = b; } if(x < min) { min = x; minIndex = size; } a[size++] = x; currIndex++; } public void pop() { if(currIndex < 0) { throw new RuntimeException("no more value to pop"); } a[currIndex--] = -1; size--; System.out.println("size: " + size); // System.out.println("currIndex: " + currIndex + ", " + minIndex + ", " + a[currIndex]); if(currIndex == minIndex) { int index = 0; int m = a[index]; for(int i = (currIndex - 1); i >= 0; i--) { for(int j = i; j >= 0; j--) { // System.out.println("a=" + m + ", " + a[j]); if(m > a[j]) { index = j; m = a[index]; } } } min = a[index]; minIndex = index; } if(size == 0) { min = Integer.MAX_VALUE; minIndex = -1; } } public int top() { System.out.println(currIndex+""); return a[currIndex-1]; } public int getMin() { return min; } public static void main(String[] args) { MinStack ms = new MinStack(); // ms.push(2);ms.push(0);ms.push(3);ms.push(0); // System.out.println(+ ms.getMin()); // ms.pop(); // System.out.println(+ ms.getMin()); // ms.pop(); // System.out.println(+ ms.getMin()); // ms.pop(); // System.out.println(+ ms.getMin()); ms.push(2147483646);ms.push(2147483646);ms.push(2147483647); System.out.println(ms.top()); ms.pop(); System.out.println(ms.getMin()); ms.pop(); System.out.println(ms.getMin()); ms.pop(); ms.push(2147483647); System.out.println(ms.top()); System.out.println(ms.getMin()); ms.push(-2147483648); System.out.println(ms.top()); System.out.println(ms.getMin()); ms.pop(); System.out.println(ms.getMin()); } }
相关文章推荐
- 实现一个栈Stack,要求实现Push(出栈)、Pop(入栈)、Min(返回最小值的操作)的时间复杂度为O(1)
- 实现一个栈,要求Push/Pop/Min(返回最小值)的时间复杂度为O(1)
- 定义一个栈的数据结构,实现min函数,要求push,pop,min时间复杂度是0(1);找出字符串中的最长子串,要求子串不含重复字符,时间复杂度是O(n);
- 实现一个栈,要求push(入栈),pop(出栈),min(返回栈中的最小值)的时间复杂度为O(1);
- 栈--实现一个栈Stack,要求实现Push(出栈)、Pop(入栈)、Min(返回最小值的操作)的时间复杂度为O(1)
- 实现一个栈,要求实现Push(出栈)、Pop(入栈)、Min(返回最小值的操作)的时间复杂度为O(1)
- 【每日一题】实现一个栈Stack,要求实现Push(出栈)、Pop(入栈)、 Min(返回最小值的操作)的时间复杂度为O(1)
- 实现一个栈(元素遵守先入后出顺序),能够通过 min 方法在 O(1)时间内获取栈中的最小元素。同时,栈的基本操作:入栈(Push)、出栈(Pop),也是在O(1)时间内完成的
- 实现一个栈,要求Push(入栈),Pop(出栈),Min(返回最小值的操作)的时间复杂度为O(1)
- 【面试题】实现一个栈要求Push,Pop,Min(返回栈中最小值的操作)的时间复杂度为O(1)
- 实现一个栈,要求实现Push(入栈)、Pop(出栈)、Min(返回最小值的操作)的时间复杂度为O(1)
- 1. 实现一个栈,要求实现 Push (出栈)、 Pop (入栈)、 Min (返回最小值的操作) 的时间复杂度为 O(1)
- 【数据结构】实现一个栈,要求实现Push(出栈)、Pop(入栈)、Min(返回最小值的操作)的时间复杂度为O(1)
- 实现一个栈Stack,要求实现Push(入栈)、Pop(出栈)、Min(返回最小值的操作)的时间复杂度为O(1)
- ~实现一个栈,要求实现Push(出栈)、Pop(入栈)、Min(返回最小值的操作)的时间复杂度为O(1)~
- 剑指offer 21---实现一个栈, 要求实现Push( 出栈) 、 Pop( 入栈) 、 Min( 返回最小值的操作) 的时间复杂度为O(1)
- 模拟实现一个Stack 要求实现Push(出栈)、Pop(入栈)、Min(返回最小值的操作)的时间复杂度为O(1)
- 面试题:定义栈的数据结构,要求添加一个min函数,能够得到栈的最小元素,要求函数min,push及pop的时间复杂度都是O(1)
- 实现一个栈Stack,要求实现Push(出栈)、Pop(入栈)、Min(返回最小值的操作)的时间复杂度为O(1)
- 定义栈的数据结构,要求添加一个min函数,能够得到栈的最小元素。要求函数min、push以及pop的时间复杂度都是O(1)。