19°

请在该类型中实现一个能够得到栈中所含最小元素的min函数

import java.util.Stack;

public class Solution {

Stack<Integer> stack1=new Stack<Integer>();

Stack<Integer> stack2=new Stack<Integer>();

public void push(int node) {
  stack1.push(node);

if(stack2.isEmpty())
    stack2.push(node);
else
    if(stack2.peek()&gt;=node)
        stack2.push(node);

}

public void pop() { if(stack1.peek()==stack2.peek()) stack2.pop(); stack1.pop();

}

public int top() { return stack1.peek(); }

public int min() { return stack2.peek(); }

}

本文由【南】发布于开源中国,原文链接:https://my.oschina.net/u/2511906/blog/3136279

全部评论: 0

    我有话说: