LeetCode Remove Duplicate Letters
原题链接在这里:https://leetcode.com/problems/remove-duplicate-letters/
题目:
Given a string which contains only lowercase letters, remove duplicate letters so that every letter appears once and only once. You must make sure your result is the smallest in lexicographical order among all possible results.
Example 1:
Input:"bcabc"
Output:"abc"
Example 2:
Input:"cbacdcbc"
Output:"acdb"
题解:
Mark the last appearance of each char in s.
For current char, if it is not added in stack.
Keep checking the last added char, if it is bigger than current char and its last appearence is after current index i. Then it could be added later.
Pop it out and mark it unused.
Eventually the chars in stack are expected result.
Time Complexity: O(n). = s.length().
Space: O(1).
AC Java:
class Solution {
public String removeDuplicateLetters(String s) {
int [] last = new int[26];
for(int i = 0; i<s.length(); i++){
last[s.charAt(i)-'a'] = i;
} Stack<Character> stk = new Stack<>();
boolean [] used = new boolean[26];
for(int i = 0; i<s.length(); i++){
char c = s.charAt(i);
if(used[c-'a']){
continue;
} while(!stk.isEmpty() && stk.peek()>c && last[stk.peek()-'a']>i){
char top = stk.pop();
used[top-'a'] = false;
} stk.push(c);
used[c-'a'] = true;
} StringBuilder sb = new StringBuilder();
for(char c : stk){
sb.append(c);
} return sb.toString();
}
}
类似Smallest Subsequence of Distinct Characters.
LeetCode Remove Duplicate Letters的更多相关文章
- [LeetCode] Remove Duplicate Letters 移除重复字母
Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...
- leetcode@ [316] Remove Duplicate Letters (Stack & Greedy)
https://leetcode.com/problems/remove-duplicate-letters/ Given a string which contains only lowercase ...
- [LeetCode] 316. Remove Duplicate Letters 移除重复字母
Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...
- 【LeetCode】316. Remove Duplicate Letters 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- Remove Duplicate Letters -- LeetCode
Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...
- leetcode 316. Remove Duplicate Letters
Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...
- 贪心: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的顺序不能改变,只能通过容器来获得由大到小的顺 ...
- 【leetcode】316. Remove Duplicate Letters
题目如下: Given a string which contains only lowercase letters, remove duplicate letters so that every l ...
- [Swift]LeetCode316. 去除重复字母 | Remove Duplicate Letters
Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...
随机推荐
- BZOJ1946 : [Ceoi2006]ANTENNA
首先通过随机增量法求出最小覆盖圆,作为答案的上界. 然后二分答案,检验的时候枚举每个点作为原点,求出其他每个点被包括在圆内的角度区间,然后扫描线即可. 时间复杂度$O(Tn^2\log n)$. #i ...
- spark-sql访问hive的问题记录
好久没有弄博客了... hive0.14 spark0.12 [hadoop@irs bin]$ ./spark-sql Spark assembly has been built with Hive ...
- 转载:冷门js技巧
前端已经被玩儿坏了!像console.log()可以向控制台输出图片等炫酷的玩意已经不是什么新闻了,像用||操作符给变量赋默认值也是人尽皆知的旧闻了,今天看到Quora上一个帖子,瞬间又GET了好多前 ...
- [深入浅出Windows 10]不同平台设备的适配
2.3 不同平台设备的适配 Windows 10通用应用程序针对特定的平台还会有一个子API的集合,当我们要使用到某个平台的特定API的时候(比如手机相机硬件按钮触发事件),这时候就需要调用特定平台的 ...
- NOIP 2012 Day2T2 借教室题解
NOIP 2012 Day2T2 借教室题解 题目传送门:http://codevs.cn/problem/1217/ 题目描述 Description 在大学期间,经常需要租借教室.大到院系举办活动 ...
- ucgui
消息机制 http://blog.csdn.net/jacklam200/article/details/5919898 font http://blog.csdn.net/jacklam200/ar ...
- HashMap封装的数据用循环快速添加进list中产生的数据集全部相同的问题
一.问题概述 在一次使用SimpleAdapter时,Data需要使用传入一条数据(Image.Text),该数据条使用HashMap封装.在用HashMap封装的数据用循环快速添加进list中产生了 ...
- IE hack中主要的几个
_: IE6; #*+.: IE6 IE7; black\0: IE8; black\9: IE所有; @media screen\9 { … }: IE6 IE7; @media \0screen\ ...
- Spring和Hibernate集成配置
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...
- *HDU3496 背包DP
Watch The Movie Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)T ...