【LeetCode OJ 225】Implement Stack using Queues

题目链接:https://leetcode.com/problems/implement-stack-using-queues/

题目:Implement the following operations of a stack using queues.

  • push(x) -- Push element x onto stack.
  • pop() -- Removes the element on top of the stack.
  • top() -- Get the top element.
  • empty() -- Return whether the stack is empty.

Notes:

  • You must use only standard operations of a queue -- which means only push
    to back
    peek/pop from frontsize,
    and is empty operations are valid.
  • Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
  • You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).

解题思路:用队列实现栈,基本思路是用两个队列模拟栈,并保持总有一个队列为空。

入栈:将元素翻入空队列中,然后将非空队列的元素添加到该队列中

出栈:将非空队列的元素出队列即可

栈顶元素:返回非空队列对头元素

判断是否为空:两个队列均为空则栈空

示例代码如下:

public class Solution
{
	Queue<Integer> q1=new ArrayDeque<>();
	Queue<Integer> q2=new ArrayDeque<>();
	  // Push element x onto stack.
    public void push(int x)
    {
      if(q1.isEmpty())
      {
    	  q1.add(x);
    	  while(!q2.isEmpty())
    	  {
    		  q1.add(q2.poll());
    	  }
      }
      else
      {
    	  q2.add(x);
    	  while(!q1.isEmpty())
    	  {
    		  q2.add(q1.poll());
    	  }
      }
    }
    // Removes the element on top of the stack.
    public void pop()
    {
    	if(!q1.isEmpty())
    		q1.poll();
    	if(!q2.isEmpty())
    		q2.poll();
    }
    // Get the top element.
    public int top()
    {
    	 if (!q1.isEmpty())
             return q1.peek();
         if (!q2.isEmpty())
             return q2.peek();
         else
        	 return -1;
    }
    // Return whether the stack is empty.
    public boolean empty()
    {
    	return q1.isEmpty() && q2.isEmpty();
    }
}
时间: 2024-11-08 22:10:29

【LeetCode OJ 225】Implement Stack using Queues的相关文章

【LeetCode OJ 232】Implement Queue using Stacks

题目链接:https://leetcode.com/problems/implement-queue-using-stacks/ 题目:Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from in front of queue. peek() -- Get the fro

【LeetCode】Implement Stack using Queues 解题报告

[题目] Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. empty() -- Return whether the stack is empty. Notes: You must use o

【LeetCode 225_数据结构_栈】Implement Stack using Queues

1 class Stack { 2 public: 3 // Push element x onto stack. 4 void push(int x) { 5 int len = nums.size(); 6 nums.push(x); 7 for (int i = 0; i < len; ++i) { 8 nums.push(nums.front()); 9 nums.pop(); 10 } 11 } 12 13 // Removes the element on top of the st

【LeetCode OJ 075】Sort Colors

题目链接:https://leetcode.com/problems/sort-colors/ 题目:Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0,

【LeetCode OJ 14】Longest Common Prefix

题目链接:https://leetcode.com/problems/longest-common-prefix/ 题目:Write a function to find the longest common prefix string amongst an array of strings. 解题思路:寻找字符串数组的最长公共前缀,将数组的第一个元素作为默认公共前缀,依次与后面的元素进行比较,取其公共部分,比较结束后,剩下的就是该字符串数组的最长公共前缀,示例代码如下: public clas

【LeetCode OJ 242】Valid Anagram

题目链接:https://leetcode.com/problems/valid-anagram/ 题目:Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = "anagram", t = "nagaram", return true. s = "rat", t = "car", re

【LeetCode OJ 004】Median of Two Sorted Arrays

题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ 题目:There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). 解题思路:将两个有序数

【LeetCode OJ 328】Odd Even Linked List

题目链接:https://leetcode.com/problems/odd-even-linked-list/ 题目:Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes. You should try to d

【LeetCode OJ 101】Symmetric Tree

题目链接:https://leetcode.com/problems/symmetric-tree/ 题目:Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / 2 2 / \ / 3 4 4 3 But the following is not: 1 / 2 2 \