求star

开源不易,喜欢请点个star吧

Ocean Han
541 字
3 分钟
Leetcode->最小栈
2022-06-14

题目#

设计一个支持 push ,pop ,top 操作,并能在常数时间内检索到最小元素的栈。

实现 MinStack 类:

  • MinStack() 初始化堆栈对象。
  • void push(int val) 将元素val推入堆栈。
  • void pop() 删除堆栈顶部的元素。
  • int top() 获取堆栈顶部的元素。
  • int getMin() 获取堆栈中的最小元素。

示例 1:

输入:
["MinStack","push","push","push","getMin","pop","top","getMin"]
[[],[-2],[0],[-3],[],[],[],[]]

输出:
[null,null,null,null,-3,null,0,-2]

解释:
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin();   --> 返回 -3.
minStack.pop();
minStack.top();      --> 返回 0.
minStack.getMin();   --> 返回 -2.

解题:tada:#

①辅助栈(使用额外空间)#

Js#

var MinStack = function() {
    this.stack = []
    this.minStack = [Infinity]
    return this
};

/** 
 * @param {number} val
 * @return {void}
 */
MinStack.prototype.push = function(val) {
    if(!this.stack.length){
        this.stack.push(val)
        this.minStack.push(val)
    }else{
        let min = this.minStack[this.minStack.length-1]
        if(val<=min){
            this.minStack.push(val)
            this.stack.push(val)
        }else{
            this.stack.push(val)
        }
    }
};

/**
 * @return {void}
 */
MinStack.prototype.pop = function() {
    if(this.stack.length){
        let min = this.minStack[this.minStack.length-1]
        let popItem = this.stack.pop()
        if(min === popItem){
            this.minStack.pop()
        }
        return popItem 
    }else{
        return undefined
    }
};

/**
 * @return {number}
 */
MinStack.prototype.top = function() {
    if(this.stack.length){
        return this.stack[this.stack.length-1]
    }else{
        return undefined
    }
};

/**
 * @return {number}
 */
MinStack.prototype.getMin = function() {
    if(this.stack.length){
        return this.minStack[this.minStack.length-1]
    }else {
        return Infinity
    }
};

/**
 * Your MinStack object will be instantiated and called as such:
 * var obj = new MinStack()
 * obj.push(val)
 * obj.pop()
 * var param_3 = obj.top()
 * var param_4 = obj.getMin()
 */

C#


//单调栈 单调递减
typedef struct 
{
    //正常 stack
    int stack[10000];
    int stackTop;

    //辅助 stack
    int minStack[10000];
    int minStackTop;
} MinStack;


MinStack* minStackCreate() 
{
    MinStack* newStack      = (MinStack *) malloc(sizeof(MinStack));
    newStack->stackTop      = 0;
    newStack->minStackTop   = 0;
    return newStack;
}

void minStackPush(MinStack* obj, int val) 
{
    //先压入数据栈
    obj->stack[obj->stackTop++] = val;
    //辅助栈空 或者 当前值小于等于 辅助栈
    if(!obj->minStackTop || val <= obj->minStack[obj->minStackTop-1] )
    {
        obj->minStack[obj->minStackTop++] = val;
    }
    
}

void minStackPop(MinStack* obj)
{
    if( obj->minStack[obj->minStackTop-1]  == obj->stack[obj->stackTop-1] )
    {
        obj->minStackTop--;
    }
    obj->stackTop--;
}

int minStackTop(MinStack* obj) 
{
    return obj->stack[obj->stackTop-1]; 
}

int minStackGetMin(MinStack* obj) 
{
    return obj->minStack[obj->minStackTop-1]; 
}

void minStackFree(MinStack* obj) 
{
    free(obj);
}

②单栈同时存储(不使用额外空间)#

TIP
  • 单栈同时存储 最小值 + 数据值 一起成套的入栈中
  • 即栈顶存放着的是 最小值 + 数据值

C#

struct stack{
    int data;
    int min;
};

typedef struct {
    struct stack stackData[10000];
    int stackTop;
} MinStack;


MinStack* minStackCreate() {
    MinStack* newStack = (MinStack*)malloc(sizeof(MinStack));
    newStack->stackTop = 0;
    return newStack;
}

void minStackPush(MinStack* obj, int val) {
    obj->stackData[obj->stackTop].data = val;
	
    // 判断是否需要更新min
    if(!obj->stackTop || val <= obj->stackData[obj->stackTop-1].min){
        obj->stackData[obj->stackTop].min = val;
    }else {
        obj->stackData[obj->stackTop].min = obj->stackData[obj->stackTop-1].min;
    }
    obj->stackTop++;
}

void minStackPop(MinStack* obj) {
    obj->stackTop--;
}

int minStackTop(MinStack* obj) {
    return obj->stackData[obj->stackTop-1].data;
}

int minStackGetMin(MinStack* obj) {
    return obj->stackData[obj->stackTop-1].min;
}

void minStackFree(MinStack* obj) {
    free(obj);
}
Leetcode->最小栈
https://blog.oceanh.top/posts/algorithm/最小栈/
作者
Ocean Han
发布于
2022-06-14
许可协议
CC BY-NC-SA 4.0
最后修改时间
2024-08-10 10:08:49