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 ...
随机推荐
- BZOJ3189 : [Coci2011]Slika
通过离线将操作建树,即可得到最终存在的操作. 然后逆着操作的顺序,倒着进行染色,对于每行维护一个并查集即可. 时间复杂度$O(n(n+m))$. #include<cstdio> cons ...
- BZOJ3212 Pku3468 A Simple Problem with Integers 题解
题目大意: 一个数列,有两个操作:1.修改操作,将一段区间内的数加上c:2.查询操作,查询一段区间内的数的和. 思路: 线段树裸题,区间修改.区间查询,维护和以及加上的数,由于无序,不需要向下推标记, ...
- 透过现象看现象(SQL501N错误处理)
某日一直运行比较正常的报表系统,突然报存储过程执行失败,查看DB2 错误返回码为sql501n,查看此错误原因如下: [db2inst1@limt ~]$ db2 ? sql501n SQL0501N ...
- Selenium_获取相对坐标
<html> <head> <title>位置</title> <style> .test { background: url(" ...
- CSS去除firefox点击链接时的虚线边框
a { outline: none; } 或者缩小范围: a:focus { outline: none; }
- [LintCode] Gray Code 格雷码
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- JDBC连接池。。。转载
1. 引言 近年来,随着Internet/Intranet建网技术的飞速发展和在世界范围内的迅速普及,计算机 应用程序已从传统的桌面应用转到Web应用.基于B/S(Browser/Server)架 ...
- mysql慢日志管理
一.日志切割 原理: 1.cp一个慢日志备份 2.清空原理的慢日志 3.写成脚本,每天一切,这样就ok啦 二.查找日志中的慢日志 1.做了日志切割(慢日志文件就小了) 2.查找某个时间的慢日志 日志时 ...
- Top Deep Learning Projects in github
Top Deep Learning Projects A list of popular github projects related to deep learning (ranked by sta ...
- print_r、echo、var_dump三者的区别
这三者均是php里输出的方式. 1.print_r用于输出数组. 2.echo只能用于输出字符串,在没有框架的时候,多用这种方法将结果打印在html页面上. 3.var_dump能输出任何类型的数据, ...