代码拉取完成,页面将自动刷新
同步操作将从 doocs/leetcode 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
Write a program to sort a stack such that the smallest items are on the top. You can use an additional temporary stack, but you may not copy the elements into any other data structure (such as an array). The stack supports the following operations: push
, pop
, peek
, and isEmpty
. When the stack is empty, peek
should return -1.
Example1:
Input: ["SortedStack", "push", "push", "peek", "pop", "peek"] [[], [1], [2], [], [], []] Output: [null,null,null,1,null,2]
Example2:
Input: ["SortedStack", "pop", "pop", "push", "pop", "isEmpty"] [[], [], [], [1], [], []] Output: [null,null,null,null,null,true]
Note:
class SortedStack:
def __init__(self):
self.s = []
def push(self, val: int) -> None:
t = []
while not self.isEmpty() and self.s[-1] < val:
t.append(self.s.pop())
self.s.append(val)
while len(t) > 0:
self.s.append(t.pop())
def pop(self) -> None:
if not self.isEmpty():
self.s.pop()
def peek(self) -> int:
return -1 if self.isEmpty() else self.s[-1]
def isEmpty(self) -> bool:
return len(self.s) == 0
class SortedStack {
private Stack<Integer> s;
public SortedStack() {
s = new Stack<>();
}
public void push(int val) {
Stack<Integer> t = new Stack<>();
while (!isEmpty() && s.peek() < val) {
t.push(s.pop());
}
s.push(val);
while (!t.isEmpty()) {
s.push(t.pop());
}
}
public void pop() {
if (!isEmpty()) {
s.pop();
}
}
public int peek() {
return isEmpty() ? -1 : s.peek();
}
public boolean isEmpty() {
return s.isEmpty();
}
}
/**
* 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();
*/
class SortedStack {
stack: number[];
constructor() {
this.stack = [];
}
push(val: number): void {
let t = [];
while (!this.isEmpty() && this.peek() < val) {
t.push(this.stack.pop());
}
this.stack.push(val);
while (t.length > 0) {
this.stack.push(t.pop());
}
}
pop(): void {
this.stack.pop();
}
peek(): number {
return this.isEmpty() ? -1 : this.stack[this.stack.length - 1];
}
isEmpty(): boolean {
return this.stack.length == 0;
}
}
/**
* Your SortedStack object will be instantiated and called as such:
* var obj = new SortedStack()
* obj.push(val)
* obj.pop()
* var param_3 = obj.peek()
* var param_4 = obj.isEmpty()
*/
type SortedStack struct {
data []int
}
func Constructor() SortedStack {
return SortedStack{make([]int, 0)}
}
func (s *SortedStack) Push(val int) {
temp := make([]int, 0)
for !s.IsEmpty() && s.Peek() < val {
temp = append(temp, s.Peek())
s.Pop()
}
s.data = append(s.data, val)
for len(temp) > 0 {
s.data = append(s.data, temp[len(temp)-1])
temp = temp[:len(temp)-1]
}
}
func (s *SortedStack) Pop() {
if !s.IsEmpty() {
s.data = s.data[:len(s.data)-1]
}
}
func (s *SortedStack) Peek() int {
if !s.IsEmpty() {
return s.data[len(s.data)-1]
}
return -1
}
func (s *SortedStack) IsEmpty() bool {
return len(s.data) == 0
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。