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. c创建win窗口

    windows程序设计示例: #include "windows.h" #pragma comment(lib, "winmm") LRESULT CALLBA ...

  2. 25-语言入门-25-n-1位数

    题目地址: http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=96    描述已知w是一个大于10但不大于1000000的无符号整数,若w是n(n ...

  3. Android XML使用的学习记录

    1. 注释其中一段代码或是一行,可以采用<!-- -->,示例如下 <!--       <EditText         android:layout_width=&quo ...

  4. awk 传入外部参数

    awk 传入外部参数 num1=1.1 num2=2.2 result=$(awk -v n1=$num1 -v n2=$num2 'BEGIN{print (n2>n1)?1:0}')

  5. c# webbrowser 清除当前网站 cookie

    //这个方法可以创建一个清除当前页面下指定域的所有cookie //必须是可以访问的域,比如你访问的是qq.com,那么可以清除www.qq.com,qzone.qq.com等页面的cookie // ...

  6. 函数flst_remove

    移除 node, node->prev直接指向node->next /*********************************************************** ...

  7. bzoj1312

    忘写题解了,经典的最大密度子图 可以类似分数规划的做,二分密度,然后转化为最大权闭合子图做,判断是否大于0 注意方案的输出 const eps=1e-6; lim=1e-12; inf=; type ...

  8. css,html命名规则

    css,html命名规则 页头: header 登录条: loginBar 标志: logo 侧栏: sideBar 广告: banner 导航: nav 子导航: subNav 菜单: menu 子 ...

  9. UVa 10596 Moring Walk【欧拉回路】

    题意:给出n个点,m条路,问能否走完m条路. 自己做的时候= =三下两下用并查集做了交,WA了一发-后来又WA了好几发--(而且也是判断了连通性的啊) 搜了题解= = 发现是这样的: 因为只要求走完所 ...

  10. asp.net实现文件解压和压缩

    C#解压RAR压缩文件(--转载--测试通过) using System; using System.Collections.Generic; using System.Text; using Sys ...