day10 | 栈与队列

0.引言

● 理论基础
● 232.用栈实现队列
● 225. 用队列实现栈

1.用栈实现队列

Category Difficulty Likes Dislikes
algorithms Easy (68.70%) 855 -

请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(pushpoppeekempty):

实现 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

提示:

  • 1 <= x <= 9
  • 最多调用 100pushpoppeekempty
  • 假设所有操作都是有效的 (例如,一个空的队列不会调用 pop 或者 peek 操作)

进阶:

  • 你能否实现每个操作均摊时间复杂度为 O(1) 的队列?换句话说,执行 n 个操作的总时间复杂度为 O(n) ,即使其中一个操作可能花费较长时间。

Discussion | Solution

1.1.自己想法及代码实现

左手倒右手,右手倒左手:

/*
 * @lc app=leetcode.cn id=232 lang=cpp
 *
 * [232] 用栈实现队列
 */

// @lc code=start
class MyQueue {
 public:
  MyQueue() {
    // data_stack_.clear();
    // tmp_stack_.clear();
  }

  void push(int x) { data_stack_.push(x); }

  int pop() {
    // 照理来说应该判断一下
    // if(data_stack_.empty()){
    //     return ;
    // }

    int res;
    while (!data_stack_.empty()) {
      tmp_stack_.push(data_stack_.top());  // top只读
      data_stack_.pop();                   // pop只出
    }

    res = tmp_stack_.top();
    tmp_stack_.pop();

    while (!tmp_stack_.empty()) {
      data_stack_.push(tmp_stack_.top());
      tmp_stack_.pop();
    }
    return res;
  }

  int peek() {
    // if(data_stack_.empty()){
    //     return ;
    // }

    int res;
    while (!data_stack_.empty()) {
      tmp_stack_.push(data_stack_.top());
      data_stack_.pop();
    }

    res = tmp_stack_.top();

    while (!tmp_stack_.empty()) {
      data_stack_.push(tmp_stack_.top());
      tmp_stack_.pop();
    }
    return res;
  }

  bool empty() { return data_stack_.empty(); }

 private:
  std::stack<int> data_stack_;
  std::stack<int> tmp_stack_;
};

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue* obj = new MyQueue();
 * obj->push(x);
 * int param_2 = obj->pop();
 * int param_3 = obj->peek();
 * bool param_4 = obj->empty();
 */
// @lc code=end

1.2.参考思想及代码实现

2.# 用队列实现栈

Category Difficulty Likes Dislikes
algorithms Easy (66.85%) 674 -

请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(pushtoppopempty)。

实现 MyStack 类:

  • void push(int x) 将元素 x 压入栈顶。
  • int pop() 移除并返回栈顶元素。
  • int top() 返回栈顶元素。
  • boolean empty() 如果栈是空的,返回 true ;否则,返回 false

注意:

  • 你只能使用队列的基本操作 —— 也就是 push to backpeek/pop from frontsizeis empty 这些操作。
  • 你所使用的语言也许不支持队列。 你可以使用 list (列表)或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。

示例:

输入:
["MyStack", "push", "push", "top", "pop", "empty"]
[[], [1], [2], [], [], []]
输出:
[null, null, null, 2, 2, false]

解释:
MyStack myStack = new MyStack();
myStack.push(1);
myStack.push(2);
myStack.top(); // 返回 2
myStack.pop(); // 返回 2
myStack.empty(); // 返回 False

提示:

  • 1 <= x <= 9
  • 最多调用100pushpoptopempty
  • 每次调用 poptop 都保证栈不为空

进阶:你能否仅用一个队列来实现栈。


Discussion | Solution

2.1.自己想法及代码实现

极限旋转,保证index永远指向栈的顶端:

/*
 * @lc app=leetcode.cn id=225 lang=cpp
 *
 * [225] 用队列实现栈
 */

// @lc code=start
class MyStack {
 public:
  MyStack() {}

  void push(int x) {
    data_.push(x);
    index_ = data_.size();
  }

  int pop() {
    int cnt = --index_;
    while (cnt--) {
      int tmp = data_.front();
      data_.pop();
      data_.push(tmp);
    }
    int res = data_.front();
    data_.pop();
    return res;
  }

  int top() {
    return data_.back();
  }

  bool empty() { return data_.empty(); }

 private:
  std::queue<int> data_;
  int index_ = 0;
};

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack* obj = new MyStack();
 * obj->push(x);
 * int param_2 = obj->pop();
 * int param_3 = obj->top();
 * bool param_4 = obj->empty();
 */
// @lc code=end

2.2.参考思想及代码实现

3.总结

  • 多画图,画几个案例基本就明了了。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容