本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/48598773

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 backpeek/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).

思路:

(1)题意为运用队列来实现栈,主要包括push()、pop()、top()、empty()方法的实现。

(2)该题的思路和“运用栈来实现队列”类似。首先还是得了解栈和队列的特性,这里再复习一遍;栈:先进后出,只能在栈顶添加和删除元素,队列:先进先出,只能在队尾添加元素,从队头移除元素。想用队列实现栈,主要需考虑到如何将先放入的元素先出栈;这里需要用两个队列来实现,其中一个队列在执行操作后为空,另一个队列存放元素。当有元素加入时,如果某一队列不为空,则将元素加入该队列中;当有元素出栈时,此时需从不为空的队列list中将除去队尾元素的其余元素全部加入另一个空的队列list0中,这样,list队列中的队尾元素(对应栈顶元素)就没有被加入到list0中,然后将list置为一个空的队列,即完成出栈操作。其余的操作比较简单,请参见代码。

(3)详情见下方代码。希望本文对你有所帮助。

算法代码实现如下:

package leetcode;

import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author liqqc
 *
 */
public class Implement_Stack_using_Queues {

	// Push element x onto stack.

	private List<Integer> list = new LinkedList<Integer>();
	private List<Integer> list0 = new LinkedList<Integer>();

	public void push(int x) {
		if (list.size() == 0 && list0.size() == 0) {
			list.add(x);
			return;
		}

		if (list.size() != 0) {
			list.add(x);
			return;
		}

		if (list0.size() != 0) {
			list0.add(x);
		}

	}

	// Removes the element on top of the stack.
	public void pop() {

		if (list.size() != 0 && list0.size() == 0) {
			for (int i = 0; i < list.size() - 1; i++) {
				list0.add(list.get(i));
			}
			list.clear();
			return;
		}

		if (list.size() == 0 && list0.size() != 0) {
			for (int i = 0; i < list0.size() - 1; i++) {
				list.add(list0.get(i));
			}
			list0.clear();
		}

	}

	// Get the top element.
	public int top() {
		if (list.size() != 0 && list0.size() == 0) {
			return list.get(list.size() - 1);
		}

		if (list.size() == 0 && list0.size() != 0) {
			return list0.get(list0.size() - 1);
		}

		return -1;
	}

	// Return whether the stack is empty.
	public boolean empty() {

		return list0.size() == 0 && list.size() == 0;
	}
}

Leetcode_252_Implement Stack using Queues的更多相关文章

  1. [LeetCode] Implement Stack using Queues 用队列来实现栈

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  2. Java for LeetCode 225 Implement Stack using Queues

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  3. Implement Stack using Queues

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  4. (leetcode)Implement Stack using Queues

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  5. (easy)LeetCode 225.Implement Stack using Queues

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

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

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

  7. 【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( ...

  8. Leetcode 225 Implement Stack using Queues

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  9. Java [Leetcode 225]Implement Stack using Queues

    题目描述: Implement the following operations of a stack using queues. push(x) -- Push element x onto sta ...

随机推荐

  1. 协议系列之IP协议

    1.协议 协议(protocol)的定义:为计算机网络中进行数据交换而建立的规则.标准或约定的集合.两个终端相互通信时双方达成的一种约定,规定了一套通信规则,双方通信必须遵守这些规则.这些规则规定了分 ...

  2. React Native组件只Image

    不管在Android还是在ios原生的开发中,图片都是作为控件给出来的,在RN中也有这么一个控件(Image).根据官网的资料,图片分为本地静态图片,网络图片和混合app资源.一下分类介绍来源官网. ...

  3. TortoiseSVN使用

    TortoiseSVN是Subversion版本控制系统的一个免费开源客户端,不需要为使用它而付费. TortoiseSVN是 Subversion 的 Windows 扩展.它使你避免接触 Subv ...

  4. Dynamics CRM2015 页面导航栏顶部全局快速查找功能配置

    在CRM2015中微软加入了新的快速查找功能,让你的数据查找更加方便,功能栏如下图所示,直接可以框中输入搜索项进行搜索. 但该功能是需要进行些配置,具体的配置在设置-管理-系统设置中,默认的就是红框中 ...

  5. testng的使用

    TestNG教程 TestNG是一个测试框架,其灵感来自JUnit和NUnit,但同时引入了一些新的功能,使其功能更强大,使用更方便. TestNG设计涵盖所有类型的测试:单元,功能,端到端,集成等, ...

  6. leetcode 3 Longest Substring Without Repeating Characters最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  7. 输入过滤器——InputFilter

    一般情况下我们通过请求体读取器InputStreamInputBuffer获取的仅仅是源数据,即未经过任何处理发送方发来的字节.但有些时候在这个读取的过程中希望做一些额外的处理,并且这些额外处理可能是 ...

  8. String压缩 解压缩

    数据传输时,有时需要将数据压缩和解压缩,本例使用GZIPOutputStream/GZIPInputStream实现. 1.使用ISO-8859-1作为中介编码,可以保证准确还原数据 2.字符编码确定 ...

  9. 精通CSS+DIV网页样式与布局--页面背景

    上篇博客,我们主要简单的总结了CSS的图片效果,我们这回来讲讲CSS如何对网页的背景进行设置,网页的背景是整个网页的重要组成部分,她直接决定了整个网页的风格和色调.这篇博客简单的总结一下如何用CSS来 ...

  10. (NO.00004)iOS实现打砖块游戏(四):砖块类的实现

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 用Xcode打开之前SpriteBuilder创建的项目,我们现 ...