栈排序。 编写程序,对栈进行排序使最小元素位于栈顶。最多只能使用一个其他的临时栈存放数据,但不得将元素复制到别的数据结构(如数组)中。该栈支持如下操作:push、pop、peek 和 isEmpty。当栈为空时,peek 返回 -1。
示例1:
输入:
[“SortedStack”, “push”, “push”, “peek”, “pop”, “peek”]
[[], [1], [2], [], [], []]
输出:
[null,null,null,1,null,2]
示例2:
输入:
[“SortedStack”, “pop”, “pop”, “push”, “pop”, “isEmpty”]
[[], [], [], [1], [], []]
输出:
[null,null,null,null,null,true]
说明:
栈中的元素数目在[0, 5000]范围内。
class SortedStack {
Stack<Integer> stack1;
Stack<Integer> stack2;
public SortedStack() {
stack1 = new Stack<>();
stack2 = new Stack<>();
}
// 在push的时候就保持有序
public void push(int val) {
if(stack1.empty()){
stack1.push(val);
}else{
while(!stack1.empty() && stack1.peek() < val){
stack2.push(stack1.pop());
}
stack1.push(val);
while(!stack2.empty()){
stack1.push(stack2.pop());
}
}
}
public void pop() {
if(!this.isEmpty()){
stack1.pop();
}
}
public int peek() {
if(this.isEmpty()) return -1;
return stack1.peek();
}
public boolean isEmpty() {
return stack1.empty();
}
}
/**
* Your SortedStack object will be instantiated and called as such:
* SortedStack obj = new SortedStack();
* obj.push(val);
* obj.pop();
* int param_3 = obj.peek();
* boolean param_4 = obj.isEmpty();
*/