【LeetCode-面试算法经典-Java实现】【155-Min Stack(最小栈)】

【155-Min Stack(最小栈)】


【LeetCode-面试算法经典-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.

题目大意

  设计一个栈,支持push,pop,top,和查找最小的元素操作(常量时间)

解题思路

  使用一个辅助栈来保存栈中的最小元素。

代码实现

算法实现类

public class MinStack {

    private final static int DEFAULT_SIZE = 1000;
    private int[] stack;
    private int[] minIndex; // 用于保存前n个数据的栈中最小元素的下标
    private int min = Integer.MAX_VALUE; // 用于记录插入过程中的最小数据
    private int index = -1; // 记录最小元素在stack中的位置
    private int current = -1;

    public MinStack() {
        stack = new int[DEFAULT_SIZE];
        minIndex = new int[DEFAULT_SIZE];
    }

    public MinStack(int size) {
        stack = new int[size];
        minIndex = new int[size];
    }

    public void push(int x) {
        current++; // 移动到要插入的位置

        if (current >= stack.length) { // 扩容
            int[] tmp = new int[current * 2];
            System.arraycopy(stack, 0, tmp, 0, stack.length);
            stack = tmp;

            tmp = new int[current * 2];
            System.arraycopy(minIndex, 0, tmp, 0, minIndex.length);
            minIndex = tmp;
        }

        stack[current] = x; // 插入数据

        if (x < min) { // 保存插入的最小值
            min = x;
            index = current; // 记录[0, current]中最小的元素下标是index
        }

        minIndex[current] = index;
    }

    public void pop() {
        current--;
        if (current >= 0) {
            min = stack[minIndex[current]]; // 重新设置栈中的最小值
            index = minIndex[current]; // 重置最小值的索引
        }
    }

    public int top() {

        if (current < 0) {
            throw new RuntimeException("No more data");
        }

        return stack[current];
    }

    public int getMin() {
        if (current < 0) {
            throw new RuntimeException("No more data");
        }

        return stack[minIndex[current]];
    }
}

评测结果

  点击图片,鼠标不释放,拖动一段位置,释放后在新的窗口中查看完整图片。

特别说明

欢迎转载,转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47828195

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-03 21:53:35

【LeetCode-面试算法经典-Java实现】【155-Min Stack(最小栈)】的相关文章

[CareerCup] 3.2 Min Stack 最小栈

3.2 How would you design a stack which, in addition to push and pop, also has a function min which returns the minimum element? Push, pop and min should all operate in O(1) time. LeetCode上的原题,请参见我之前的博客Min Stack 最小栈.

lintcode 中等题:Min stack 最小栈

题目 带最小值操作的栈 实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值. 你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成. 解题 可以定义一个数组或者其他的存储最小值,第i个元素,表示栈中前i个元素的最小值. 定义两个ArrayList来存储栈,一个ArrayList存储当前栈中的元素,一个ArrayList存储最小栈,并且其第i个元素表示栈中前i个元素的最小值,这样两个栈的长度是始终一样的 入栈:最小栈需要加入的元素是 当前要入的元

[LintCode] Min Stack 最小栈

Implement a stack with min() function, which will return the smallest number in the stack. It should support push, pop and min operation all in O(1) cost. Notice min operation will never be called if there is no number in the stack. Have you met this

155. 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 e

【LeetCode-面试算法经典-Java实现】【064-Minimum Path Sum(最小路径和)】

[064-Minimum Path Sum(最小路径和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 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

【LeetCode-面试算法经典-Java实现】【120-Triangle(三角形)】

[120-Triangle(三角形)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4

【LeetCode-面试算法经典-Java实现】【121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)】

[121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one

【LeetCode-面试算法经典-Java实现】【223-Rectangle Area(矩形区域)】

[223-Rectangle Area(矩形区域)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 代码下载[https://github.com/Wang-Jun-Chao] 原题 Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined by its bottom left corner and top right corner

【LeetCode-面试算法经典-Java实现】【014-Longest Common Prefix(最长公共前缀)】

[014-Longest Common Prefix(最长公共前缀)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Write a function to find the longest common prefix string amongst an array of strings. 题目大意 写一个函数找出一个字串所数组中的最长的公共前缀. 解题思路 第一步先找出长度最小的字符串,然后将这个字符串与其它的字符串相比找出最短的最公共前缀. 代码实现 publi

【LeetCode-面试算法经典-Java实现】【011-ContainerWithMostWater(容纳最多的水)】

[011-ContainerWithMostWater(容纳最多的水)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given n non-negative integers a1, a2, -, an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai