剑指Offer_20 发表于 2017-12-03 | 分类于 剑指Offer 题目定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数。 解题思路一道,感觉怪怪的题目 12345678910111213141516171819202122232425262728293031323334353637383940414243import java.util.Stack;public class Solution { Stack<Integer> dataStack = new Stack<>(); Stack<Integer> minStack = new Stack<>(); public void push(int node) { dataStack.push(node); if(minStack.isEmpty()){ minStack.push(node); } else{ if(node <= minStack.peek()){ minStack.push(node); } } } public void pop() { if(!dataStack.isEmpty()){ if(minStack.peek() == dataStack.pop()){ minStack.pop(); } } } public int top() { if(!dataStack.isEmpty()){ return dataStack.pop(); } return -1; } public int min() { if(!minStack.isEmpty()){ return minStack.peek(); } else{ } return -1; }}