JavaScript / TypeScript / PHP / GO / Python / C++ / C# / Java 用栈实现队列,求解《232. 用栈实现队列》

2022-06-26 17:52:06
JavaScript / TypeScript / PHP / GO / Python / C++ / C# / Java 用栈实现队列,求解《232. 用栈实现队列》

例题

232. 用栈实现队列

请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty):
实现 MyQueue 类:
void push(int x) 将元素 x 推到队列的末尾
int pop() 从队列的开头移除并返回元素
int peek() 返回队列开头的元素
boolean empty() 如果队列为空,返回 true ;否则,返回 false
说明:
你 只能 使用标准的栈操作 —— 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。
你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
示例 1:
输入:
["MyQueue", "push", "push", "peek", "pop", "empty"]
[[], [1], [2], [], [], []]
输出:
[null, null, null, 1, 1, false]
解释:
MyQueue myQueue = new MyQueue();
myQueue.push(1); // queue is: [1]
myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
myQueue.peek(); // return 1
myQueue.pop(); // return 1, queue is [2]
myQueue.empty(); // return false

答案

栈 · 队列

var MyQueue = function() {
  this.stack1 = []
  this.stack2 = []
  this.front = null
};

MyQueue.prototype.push = function(x) {
  if (this.stack1.length === 0) this.front = x
  this.stack1.push(x)
};

MyQueue.prototype.pop = function() {
  if (this.stack2.length === 0) while (this.stack1.length) this.stack2.push(this.stack1.pop())
  return this.stack2.pop()
};

MyQueue.prototype.peek = function() {
  if (this.stack2.length) return this.stack2[this.stack2.length - 1]
  return this.front
};

MyQueue.prototype.empty = function() {
  return this.stack1.length === 0 && this.stack2.length === 0
};
class MyQueue {
  stack1 = []
  stack2 = []
  front = 0
  constructor() {}

  push(x: number): void {
    if (this.stack1.length === 0) this.front = x
    this.stack1.push(x)
  }

  pop(): number {
    if (this.stack2.length === 0) {
      while (this.stack1.length) this.stack2.push(this.stack1.pop())
    }
    return this.stack2.pop()
  }

  peek(): number {
    if (this.stack2.length) return this.stack2[this.stack2.length - 1]
    return this.front
  }

  empty(): boolean {
    return this.stack1.length === 0 && this.stack2.length === 0
  }
}
class MyQueue {
  private $stack1 = [];
  private $stack2 = [];
  private $front = NULL;
  function __construct() {}

  function push($x) {
    if (count($this->stack1) === 0) $this->front = $x;
    $this->stack1 []= $x;
  }

  function pop() {
    if (count($this->stack2) === 0) {
      while (count($this->stack1)) $this->stack2 []= array_pop($this->stack1);
    }
    return array_pop($this->stack2);
  }

  function peek() {
    if (count($this->stack2)) return $this->stack2[count($this->stack2) - 1];
    return $this->front;
  }

  function empty() {
    return count($this->stack1) === 0 && count($this->stack2) === 0;
  }
}
type MyQueue struct {
  stack1 []int
  stack2 []int
  front int
}

func Constructor() MyQueue {
  return MyQueue{}
}

func (this *MyQueue) Push(x int)  {
  if len(this.stack1) == 0 {
    this.front = x 
  }
  this.stack1 = append(this.stack1, x)
}

func (this *MyQueue) Pop() int {
  if len(this.stack2) == 0 {
    for len(this.stack1) > 0 {
      this.stack2 = append(this.stack2, this.stack1[len(this.stack1) - 1])
      this.stack1 = this.stack1[:len(this.stack1) - 1]
    }
  }
  t := this.stack2[len(this.stack2) - 1]
  this.stack2 = this.stack2[:len(this.stack2) - 1]
  return t
}

func (this *MyQueue) Peek() int {
  if len(this.stack2) > 0 {
    return this.stack2[len(this.stack2) - 1]
  }
  return this.front
}

func (this *MyQueue) Empty() bool {
  return len(this.stack1) == 0 && len(this.stack2) == 0
}
class MyQueue:

    def __init__(self):
      self.stack1 = []
      self.stack2 = []
      self.front = 0

    def push(self, x: int) -> None:
      if len(self.stack1) == 0: self.front = x
      self.stack1.append(x)

    def pop(self) -> int:
      if len(self.stack2) == 0:
        while len(self.stack1) > 0: self.stack2.append(self.stack1.pop())
      return self.stack2.pop() 

    def peek(self) -> int:
      if len(self.stack2) > 0: return self.stack2[-1]
      return self.front

    def empty(self) -> bool:
      return len(self.stack1) == 0 and len(self.stack2) == 0

class MyQueue {
  private Stack<Integer> stack1 = new Stack<Integer>();
  private Stack<Integer> stack2 = new Stack<Integer>();
  private Integer front;
  public MyQueue() {}

<br>
  public void push(int x) {
    if (stack1.size() == 0) front = x;
    stack1.push(x);
  }

  public int pop() {
    if (stack2.size() == 0) {
      while (stack1.empty() == false) stack2.push(stack1.pop());
    }
    return stack2.pop();
  }

  public int peek() {
    if (stack2.size() > 0) return stack2.peek();
    return front;
  }

  public boolean empty() {
    return stack1.size() == 0 && stack2.size() == 0;
  }
}
class MyQueue {
private:
  stack<int> stack1, stack2;
  int front;
public:
  MyQueue() {}

  void push(int x) {
    if (stack1.size() == 0) front = x;
    stack1.push(x);
  }

  int pop() {
    if (stack2.size() == 0) {
      while (stack1.empty() == false) {
        stack2.push(stack1.top());
        stack1.pop();
      }
    }
    int t = stack2.top();
    stack2.pop();
    return t;
  }

  int peek() {
    if (stack2.size() > 0) return stack2.top();
    return front;
  }

  bool empty() {
    return stack1.size() == 0 && stack2.size() == 0;
  }
};
public class MyQueue {
  Stack<int> stack1 = new Stack<int>();
  Stack<int> stack2 = new Stack<int>();
  int front;
  public MyQueue() {}

  public void Push(int x) {
    if (stack1.Count == 0) front = x;
    stack1.Push(x);
  }

  public int Pop() {
    if (stack2.Count == 0) {
      while (stack1.Count > 0) stack2.Push(stack1.Pop());
    }
    return stack2.Pop();
  }

  public int Peek() {
    if (stack2.Count > 0) return stack2.Peek();
    return front;
  }

  public bool Empty() {
    return stack1.Count == 0 && stack2.Count == 0;
  }
}

递归,迭代 2 种方法后序遍历,求解《1022. 从根到叶的二进制数之和》
递归,迭代(单栈,Java 用 ArrayDeque 实现)2种方法后序遍历,求解《1022. 从根到叶的二进制数之和》
栈、计数:2 解法求解《1021. 删除最外层的括号》
栈、计数,2 解法求解《1021. 删除最外层的括号》
广度优先搜索:求解《675. 为高尔夫比赛砍树》
广度优先搜索,求解《675. 为高尔夫比赛砍树》