首页 > 试题广场 >

包含min函数的栈

[编程题]包含min函数的栈
  • 热度指数:649770 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的 min 函数,输入操作时保证 poptopmin 函数操作时,栈中一定有元素。

此栈包含的方法有:
push(value):将value压入栈中
pop():弹出栈顶元素
top():获取栈顶元素
min():获取栈中最小元素

数据范围:操作数量满足 ,输入的元素满足
进阶:栈的各个操作的时间复杂度是 ,空间复杂度是

示例:
输入:    ["PSH-1","PSH2","MIN","TOP","POP","PSH1","TOP","MIN"]
输出:    -1,2,1,-1
解析:
"PSH-1"表示将-1压入栈中,栈中元素为-1
"PSH2"表示将2压入栈中,栈中元素为2,-1
MIN”表示获取此时栈中最小元素==>返回-1
"TOP"表示获取栈顶元素==>返回2
"POP"表示弹出栈顶元素,弹出2,栈中元素为-1
"PSH1"表示将1压入栈中,栈中元素为1,-1
"TOP"表示获取栈顶元素==>返回1
MIN”表示获取此时栈中最小元素==>返回-1

示例1

输入

 ["PSH-1","PSH2","MIN","TOP","POP","PSH1","TOP","MIN"]

输出

-1,2,1,-1
推荐
Ron头像 Ron
import java.util.Stack;
import java.util.Arrays;
public class Solution {
/*借用辅助栈存储min的大小,自定义了栈结构
*/
    private int size;
    private int min = Integer.MAX_VALUE;
    private Stack<Integer> minStack = new Stack<Integer>();
    private Integer[] elements = new Integer[10];
    public void push(int node) {
        ensureCapacity(size+1);
        elements[size++] = node;
        if(node <= min){
            minStack.push(node);
            min = minStack.peek();
        }else{
            minStack.push(min);
        }
    //    System.out.println(min+"");
    }

    private void ensureCapacity(int size) {
        // TODO Auto-generated method stub
        int len = elements.length;
        if(size > len){
            int newLen = (len*3)/2+1; //每次扩容方式
            elements = Arrays.copyOf(elements, newLen);
        }
    }
    public void pop() {
        Integer top = top();
        if(top != null){
            elements[size-1] = (Integer) null;
        }
        size--;
        minStack.pop();    
        min = minStack.peek();
    //    System.out.println(min+"");
    }

    public int top() {
        if(!empty()){
            if(size-1>=0)
                return elements[size-1];
        }
        return (Integer) null;
    }
    public boolean empty(){
        return size == 0;
    }

    public int min() {
        return min;
    }
} 

编辑于 2015-06-19 17:57:04 回复(57)
这题用js,真的巨简单!!!!
var stack = []
function push(node)
{
    stack.unshift(node)
}
function pop()
{
    stack.shift()
}
function top()
{
    // write code here
    if(stack[0]!=undefined){
         return stack[0]
    }
   
}
function min()
{
    // write code here
    return Math.min(...stack)
}
module.exports = {
    push : push,
    pop : pop,
    top : top,
    min : min
};


发表于 2023-09-23 16:41:56 回复(0)
let arr = [];
function push(node) {
    // write code here
    arr.push(node);
}
function pop() {
    // write code here
    if (arr.length > 0) {
        return arr.pop();
    }
}
function top() {
    // write code here
    if (arr.length > 0) {
        return arr[arr.length - 1];
    }
}
function min() {
    // write code here
    if (arr.length > 0) {
        return Math.min.apply(null, arr);
    }
}
module.exports = {
    push : push,
    pop : pop,
    top : top,
    min : min
};

发表于 2022-01-04 09:54:08 回复(0)
//这么少给JS的   来一个通俗易懂的版本吧
let stack1 = []
let stack2 = []
function push(node)
{
    // write code here
    stack1.push(node)
    if(stack2.length===0||stack2[stack2.length-1]>=node){
        stack2.push(node)
    }
}
function pop()
{
    // write code here
    if(stack1.pop()===stack2[stack2.length-1]){
        stack2.pop()
    }
}
function top()
{
    // write code here
    return stack1[stack1.length-1]
}
function min()
{
    // write code here
    return stack2[stack2.length-1]
}
module.exports = {
    push : push,
    pop : pop,
    top : top,
    min : min
};
发表于 2021-10-27 09:20:15 回复(0)
简洁的JavaScript:
let stack = [];
let minStack = [] ;

function push(node)
{
    // write code here
    stack.push(node)
    if (minStack.length === 0) minStack.push(node);
    else if (node <= min()) minStack.push(node);
    else minStack.push(min());
}
function pop()
{
    // write code here
    minStack.pop()
    return stack.pop()
}
function top()
{
    // write code here
    return stack[stack.length - 1]
}
function min()
{
    // write code here
    return minStack[minStack.length - 1]
}


发表于 2021-09-29 17:26:59 回复(0)

Js 用数组。pop出来还要再压进去,没人写js题解,我来搞一个

let s1=[],s2=[]

function push(node)
{
    // write code here
    let temp=s2.pop()
    if(!s2.length||node<=temp){
        s2.push(temp)
        s2.push(node)
    }else{
        s2.push(temp)
    }
    s1.push(node)
}
function pop()
{
    // write code here
    let temp1=s1.pop()
    let temp2=s2.pop()
    if(temp1!==temp2)
        s2.push(temp2)
}
function top()
{
    // write code here
    let temp=s1.pop()
    s1.push(temp)
    return temp
}
function min()
{
    // write code here
    let temp=s2.pop()
    s2.push(temp)
    return temp
}
module.exports = {
    push : push,
    pop : pop,
    top : top,
    min : min
};
发表于 2021-09-29 10:20:39 回复(0)