Leetcode_252_Implement Stack using Queues

97 篇文章 0 订阅
94 篇文章 18 订阅

本文是在学习中的总结,欢迎转载但请注明出处: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;
	}
}



题目描述: 给定一个字符串,请将字符串里的字符按照出现的频率降序排列。 示例 1: 输入: "tree" 输出: "eert" 解释: 'e'出现两次,'r'和't'都只出现一次。因此'e'必须出现在'r'和't'之前。此外,"eetr"也是一个有效的答案。 示例 2: 输入: "cccaaa" 输出: "cccaaa" 解释: 'c'和'a'都出现三次。此外,"aaaccc"也是有效的答案。注意"cacaca"是不正确的,因为相同的字母必须放在一起。 示例 3: 输入: "Aabb" 输出: "bbAa" 解释: 此外,"bbaA"也是一个有效的答案,但"Aabb"是不正确的。注意'A'和'a'被认为是两种不同的字符。 Java代码如下: ``` import java.util.*; public class Solution { public String frequencySort(String s) { if (s == null || s.length() == 0) { return ""; } Map<Character, Integer> map = new HashMap<>(); for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); map.put(c, map.getOrDefault(c, 0) + 1); } List<Map.Entry<Character, Integer>> list = new ArrayList<>(map.entrySet()); Collections.sort(list, (o1, o2) -> o2.getValue() - o1.getValue()); StringBuilder sb = new StringBuilder(); for (Map.Entry<Character, Integer> entry : list) { char c = entry.getKey(); int count = entry.getValue(); for (int i = 0; i < count; i++) { sb.append(c); } } return sb.toString(); } } ``` 解题思路: 首先遍历字符串,使用HashMap记录每个字符出现的次数。然后将HashMap转换为List,并按照出现次数从大到小进行排序。最后遍历排序后的List,将每个字符按照出现次数依次添加到StringBuilder中,并返回StringBuilder的字符串形式。 时间复杂度:O(nlogn),其中n为字符串s的长度。遍历字符串的时间复杂度为O(n),HashMap和List的操作时间复杂度均为O(n),排序时间复杂度为O(nlogn),StringBuilder操作时间复杂度为O(n)。因此总时间复杂度为O(nlogn)。 空间复杂度:O(n),其中n为字符串s的长度。HashMap和List的空间复杂度均为O(n),StringBuilder的空间复杂度也为O(n)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值