1.题目描述

2.思路

  思路简单,这里用一个图来举例说明:

3.java代码

 public class MyQueue {

    Stack<Integer> stack1=new Stack<Integer>();
Stack<Integer> stack2=new Stack<Integer>(); /** Push element x to the back of queue. */
public void push(int x) {
stack1.push(x);
} /** Removes the element from in front of queue and returns that element. */
public int pop() {
if(!stack2.isEmpty())
return stack2.pop();
else{
while(!stack1.empty())
stack2.push(stack1.pop());
return stack2.pop();
}
} /** Get the front element. */
public int peek() {
if(!stack2.isEmpty())
return stack2.peek();
else{
while(!stack1.empty())
stack2.push(stack1.pop());
return stack2.peek();
}
} /** Returns whether the queue is empty. */
public boolean empty() {
return stack1.empty()&&stack2.empty();
}
} /**
* 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();
* boolean param_4 = obj.empty();
*/

【LeetCode232】 Implement Queue using Stacks★的更多相关文章

  1. 【LeetCode OJ 232】Implement Queue using Stacks

    题目链接:https://leetcode.com/problems/implement-queue-using-stacks/ 题目:Implement the following operatio ...

  2. 【LeetCode 232_数据结构_队列_实现】Implement Queue using Stacks

    class Queue { public: // Push element x to the back of queue. void push(int x) { while (!nums.empty( ...

  3. 【LeetCode】232 & 225 - Implement Queue using Stacks & Implement Stack using Queues

    232 - Implement Queue using Stacks Implement the following operations of a queue using stacks. push( ...

  4. leetcode:Implement Stack using Queues 与 Implement Queue using Stacks

    一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...

  5. 232. Implement Queue using Stacks,225. Implement Stack using Queues

    232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...

  6. leetcode 155. Min Stack 、232. Implement Queue using Stacks 、225. Implement Stack using Queues

    155. Min Stack class MinStack { public: /** initialize your data structure here. */ MinStack() { } v ...

  7. Lintcode: Implement Queue by Stacks 解题报告

    Implement Queue by Stacks 原题链接 : http://lintcode.com/zh-cn/problem/implement-queue-by-stacks/# As th ...

  8. Leetcode 232 Implement Queue using Stacks 和 231 Power of Two

    1. 232 Implement Queue using Stacks 1.1 问题描写叙述 使用栈模拟实现队列.模拟实现例如以下操作: push(x). 将元素x放入队尾. pop(). 移除队首元 ...

  9. LeetCode 232. 用栈实现队列(Implement Queue using Stacks) 4

    232. 用栈实现队列 232. Implement Queue using Stacks 题目描述 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从 ...

随机推荐

  1. importToMbtiles

    import sqlite3, sys, logging, time, os, json, zlib, re ''' MapDBImporter-latest -f png -mName " ...

  2. Mobile First! Wijmo 5 之 架构

    CSDN移动开发频道率先报道的<Mobile First!jQuery UI组件集Wijmo五年最大更新>引起开发者极大关注. 本文就开发者关心的话题之一架构,展开叙述. Wijmo 5是 ...

  3. OpenCV 填充(ROI)+模糊操作

    1.ROI 操作 src = cv.imread('./1.jpg') cv.imshow('src',src) dst = src[40:240,100:300] gray = cv.cvtColo ...

  4. Android--px(像素)和dp、sp之间的相互转化

    public class DensityUtil { public DensityUtil() { } public static int dip2px(Context var0, float var ...

  5. Python中DataFrame去重

    # 去除重复行数据 keep:'first':保留重复行的第一行,'last':保留重复行的最后一行,False:删除所有重复行df = df.drop_duplicates( subset=['YJ ...

  6. 使用wxpy自动发送微信消息(加强版)

    通过使用wxpy自动发送微信消息后,笔者又加强了发送消息,堪称消息爆炸式发送 目前设置的为10秒发送一次,发送9次,每次发送10条内容 import requests import wxpy from ...

  7. element-ui的回调函数Events的用法

    做轮播的时候想用这个change回调函数,但是官方文档上竟然就只列了这么一行东西,完全没有示例代码(也可能我没找到哈) 鼓捣了半天,东拼西凑终于找到了靠谱的使用方法,其实很简单 在轮播组件上加上@ch ...

  8. windows防火墙安全设置指定ip访问指定端口

    场景摘要: 1.我有三台腾讯云服务器 2.我日常办公网络的ip换了 3.我在腾讯云上面改了安全规则,也不能访问我A服务器的21,1433等端口 4.开始我以为是办公网络的安全设置问题 5.我进B服务器 ...

  9. Windows:添加、删除和修改静态路由

    添加一条路由 route add 删除一条路由 route add -p #作用同上,只是这是一条永久路由,不会因为重启机器而丢失.  注意:如果有两条路由记录有着相同的“目的网络号”,则会将两条记录 ...

  10. 转:tcpdump抓包分析(强烈推荐)

    转自:https://mp.weixin.qq.com/s?__biz=MzAxODI5ODMwOA==&mid=2666539134&idx=1&sn=5166f0aac71 ...