包含min函数的栈

来源:互联网 发布:intro.js 编辑:程序博客网 时间:2024/05/22 23:23

包含min函数的栈
  • 时间限制:1秒空间限制:32768K
  • 本题知识点: 栈

题目描述

定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数。
import java.util.Stack;public class Solution {    Stack<Integer> stack =new Stack<>();    Stack<Integer> stackMin=new Stack<>();    public void push(int node) {        stack.push(node);        if(stackMin.isEmpty()){            stackMin.push(node);        }else{            if(stackMin.peek()>node){                stackMin.push(node);            }else{                stackMin.push(stackMin.peek());            }        }    }    public void pop() {        stackMin.pop();        stack.pop();    }    public int top() {        return stack.peek();    }    public int min() {        return stackMin.peek();    }}



0 0
原创粉丝点击