码迷,mamicode.com
首页 > 其他好文 > 详细

实现一个最小栈的push(int x),pop(),top(),min()方法,要求都是常量时间

时间:2014-12-05 15:35:03      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:java自定义栈

原题目: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.

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.

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());
    }
    
}

实现一个最小栈的push(int x),pop(),top(),min()方法,要求都是常量时间

标签:java自定义栈

原文地址:http://blog.csdn.net/startupmount/article/details/41747719

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!