https://leetcode.com/problems/remove-duplicate-letters/

Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your result is the smallest in lexicographical order among all possible results.

Example:

Given "bcabc"
Return "abc"

Given "cbacdcbc"
Return "acdb"

public class Solution {
public String removeDuplicateLetters(String s) { int[] remain = new int[26];
for(int i=0; i<s.length(); ++i) {
remain[s.charAt(i) - 'a']++;
} LinkedList<Character> stack = new LinkedList<Character> ();
HashSet<Character> onStack = new HashSet<Character> ();
for(int i=0; i<s.length(); ++i) {
char ch = s.charAt(i);
if(onStack.contains(ch)) {
remain[ch-'a']--;
continue;
} while(!stack.isEmpty() && stack.peekFirst() > ch && remain[stack.peekFirst()-'a'] > 0) {
onStack.remove(stack.peekFirst());
// System.out.println("poll " + stack.peekFirst() + " out of stack!");
stack.pollFirst();
} remain[ch-'a']--;
stack.addFirst(ch);
// System.out.println("add " + ch + " into stack!");
onStack.add(ch);
} StringBuffer sb = new StringBuffer();
while(!stack.isEmpty()) {
char ch = stack.pollLast();
sb.append(ch);
}
return sb.toString();
}
}

leetcode@ [316] Remove Duplicate Letters (Stack & Greedy)的更多相关文章

  1. [LeetCode] 316. Remove Duplicate Letters 移除重复字母

    Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...

  2. leetcode 316. Remove Duplicate Letters

    Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...

  3. 贪心:leetcode 870. Advantage Shuffle、134. Gas Station、452. Minimum Number of Arrows to Burst Balloons、316. Remove Duplicate Letters

    870. Advantage Shuffle 思路:A数组的最大值大于B的最大值,就拿这个A跟B比较:如果不大于,就拿最小值跟B比较 A可以改变顺序,但B的顺序不能改变,只能通过容器来获得由大到小的顺 ...

  4. 【LeetCode】316. Remove Duplicate Letters 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  5. 【leetcode】316. Remove Duplicate Letters

    题目如下: Given a string which contains only lowercase letters, remove duplicate letters so that every l ...

  6. 316. Remove Duplicate Letters (accumulate -> count of the difference elements in a vector)

    Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...

  7. 316. Remove Duplicate Letters

    Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...

  8. LeetCode题目: Remove Duplicate Letters

    问题描述 给一个字符串(只包含小写字母),删除重复的字母, 使得每个字母只出现一次.返回的结果必须是字典顺序最小的. 举例:“bcabc" -> "abc", &q ...

  9. 316 Remove Duplicate Letters 去除重复字母

    给定一个仅包含小写字母的字符串,去除重复的字母使得所有字母出现且仅出现一次.你必须保证返回结果是所有可能结果中的以字典排序的最短结果.例如:给定 "bcabc"返回 "a ...

随机推荐

  1. Wireshark抓包分析HTTPS与HTTP报文的差异

    一.什么是HTTPS: HTTPS(Secure Hypertext Transfer Protocol)安全超文本传输协议 它是一个安全通信通道,它基于HTTP开发,用于在客户计算机和服务器之间交换 ...

  2. 简单的算法题, Find Minimum in Rotated Sorted Array 的Python实现。

    简单的算法题, Find Minimum in Rotated Sorted Array 的Python实现. 题目: Suppose a sorted array is rotated at som ...

  3. HS-T912 adb 连接配置

    手机丢了,花300大洋买的新手机阿...不讨论好不好用,只说怎么用. 安装驱动 linux 跳过 插上电脑,在__通知__里面打开__USB 连接__,会弹出__USB 设置__对话框. 选择__海信 ...

  4. Python 中的isinstance函数

    解释: Python 中的isinstance函数,isinstance是Python中的一个内建函数 语法: isinstance(object, classinfo) 如果参数object是cla ...

  5. GridView官方教程及示例

    Grid View GridView is a ViewGroup that displays items in a two-dimensional, scrollable grid. The gri ...

  6. static,this,抽象类,接口和包

    1. static 1)静态变量:Java虚拟机为静态变量开辟单独的存储空间,所以所有的对象内部的静态变量在内存中都指向同一个地址,那么不管哪个对象改变这个成员变量,所有对象中该成员变量的值都发生变化 ...

  7. Android Studio修改包名和applicationId的方法

    背景: 如果新做的项目跟以前做的某一个项目十分相似,那么一个简单的方法就是把原来项目拷贝一份,然后修改代码,但是这样包名还是原来项目的包名,还有如果想在同一台手机上同时安装新做的app和原来的app会 ...

  8. Delphi使用JSON解析调用淘宝IP地址库REST API 示例

    淘宝IP地址库:http://ip.taobao.com,里面有REST API 说明. Delphi XE 调试通过,关键代码如下: var IdHTTP: TIdHTTP; RequestURL: ...

  9. ASP.NET MVC 学习1、新增Controller,了解MVC运行机制

    1,turorial ,根据链接教程新建一个MVC项目 http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/ ...

  10. Asp.Net Unix时间戳和DateTime类型转换

    using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System. ...