Remove Duplicate Letters I & II
Remove Duplicate Letters I
Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once.
Example:
Given "bcabc"
Return "abc"
Given "cbacdcbc"
Return "abcd"
public class Solution {
public String removeDuplicateLetters(String s) {
if (s == null || s.length() <= )
return s; int res = ;
for (int i = ; i < s.length(); i++) {
res = res | ( << s.charAt(i) - 'a');
} StringBuilder sb = new StringBuilder();
int k = ;
for (int i = ; i < ; i++) {
if ((res & (k << i)) != ) {
sb.append((char) ('a' + i));
}
}
return sb.toString();
}
}
Remove Duplicate Letters II
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"
分析:https://segmentfault.com/a/1190000004188227
这道题要保证顺序要一致,而且还是最小的。
读字符的过程中,把字符存到stack里,当发现stack之前存的字符中比当前字符大 (这是一个很好的思路,如果当前字符或者数字需要与前面的字符或者数字比较,并且比较结果和以前的状态不一样,可以考虑用stack。)而且频率还大于0就可以把那个字符pop出去。类似这种题目都可以用stack解决。基本思想就是在一定的限制条件下pop出比当前选择差的元素。
public class Solution {
public String removeDuplicateLetters(String s) {
int[] freqs = new int[]; // 统计字符频率
for (int i = ; i < s.length(); i++) {
freqs[s.charAt(i)]++;
} boolean[] visited = new boolean[]; // 用来标记存在stack里的字符
Deque<Character> q = new ArrayDeque<>(); for (int i = ; i < s.length(); i++) {
char c = s.charAt(i);
freqs[c]--;
if (visited[c]) continue; // pop出stack当中比当前字符大但后面还存在的的字符,
while (!q.isEmpty() && q.peek() > c && freqs[q.peek()] > ) {
visited[q.pop()] = false;
}
q.push(c);
visited[c] = true;
} StringBuilder sb = new StringBuilder();
for (char c : q) {
sb.append(c);
} return sb.reverse().toString();
}
}
Remove Duplicate Letters I & II的更多相关文章
- Remove Duplicate Letters
316. Remove Duplicate Letters Total Accepted: 2367 Total Submissions: 12388 Difficulty: Medium Given ...
- [Swift]LeetCode316. 去除重复字母 | Remove Duplicate Letters
Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...
- [LeetCode] Remove Duplicate Letters 移除重复字母
Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...
- 316. Remove Duplicate Letters
Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...
- LeetCode Remove Duplicate Letters
原题链接在这里:https://leetcode.com/problems/remove-duplicate-letters/ 题目: Given a string which contains on ...
- leetcode@ [316] Remove Duplicate Letters (Stack & Greedy)
https://leetcode.com/problems/remove-duplicate-letters/ Given a string which contains only lowercase ...
- Remove Duplicate Letters(Java 递归与非递归)
题目介绍: Given a string which contains only lowercase letters, remove duplicate letters so that every l ...
- 【lintcode】834. Remove Duplicate Letters
题目描述: Given a string which contains only lowercase letters, remove duplicate letters so that every l ...
- 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 ...
随机推荐
- RL中“#” “?” &“”号的作用
阅读目录 1. # 2. ? 3. & 回到顶部 1. # 10年9月,twitter改版.一个显著变化,就是URL加入了"#!"符号.比如,改版前的用户主页网址为http ...
- Java8初体验(一)lambda表达式语法
感谢同事[天锦]的投稿.投稿请联系 tengfei@ifeve.com 本文主要记录自己学习Java8的历程,方便大家一起探讨和自己的备忘.因为本人也是刚刚开始学习Java8,所以文中肯定有错误和理解 ...
- uname是什么?
uname= unix +name, 是指unix 这个操作系统的 名字, 包括 主机名, 内核版本 架构 平台名称等等
- Todd's Matlab讲义第5讲:二分法和找根
二分法和if ... else ... end 语句 先回顾一下二分法.要求方程\(f(x)=0\)的根.假设\(c = f(a) < 0\)和\(d = f(b) > 0\),如果\(f ...
- sqoop
http://blog.csdn.net/yfkiss/article/details/8700480 http://www.cnblogs.com/admln/p/sqoop1-99-4-javaa ...
- Hadoop Fsimage 和 editlog
在<Hadoop NameNode元数据相关文件目录解析>文章中提到NameNode的$dfs.namenode.name.dir/current/文件夹的几个文件: 1 current/ ...
- 全栈工程师是不是必须得会HTML5?
最近看了很多招聘,都要求前端工程师必须得会HTML5.个人觉得只要会了html,HTML5不会太难,不过全栈工程师要求会的太多了 HTML CSS JavaScript HTML 5 CSS 3 都说 ...
- Timestamp 使用
Timestamp是一个长整形的类型 1.使用方法一 Timestamp nowdate1 = new Timestamp(System.currentTimeMillis()); System.ou ...
- Swift语法入门
正文参考: https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Progra ...
- springmvc之DispatcherServlet
1.作用:DispatcherServlet是前置控制器,配置在web.xml(因为DispatcherServlet是一个servelet)文件中的.拦截匹配的请求,Servlet拦截匹配规则要自已 ...