题目:
栈排序。 编写程序,对栈进行排序使最小元素位于栈顶。最多只能使用一个其他的临时栈存放数据,但不得将元素复制到别的数据结构(如数组)中。该栈支持如下操作: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]
代码实现:
class SortedStack {标签:yyds,peek,金典,pop,LeetCode,isEmpty,push,null,stack From: https://blog.51cto.com/u_13321676/5950842
Stack<Integer> stack;
public SortedStack() {
stack = new Stack<Integer>();
}
public void push(int val) {
sort(val);
}
public void pop() {
if(stack.isEmpty()){
return;
}
stack.pop();
return;
}
public int peek() {
if(stack.isEmpty()){
return -1;
}
return stack.peek();
}
public boolean isEmpty() {
return stack.isEmpty();
}
private void sort(int val) {
if (stack.isEmpty()||val < stack.peek()){
stack.push(val);
return;
}else{
int temp = stack.pop();
sort(val);
stack.push(temp);
}
}
}