Middle-题目98:316. Remove Duplicate Letters
2016-05-31 19:52
246 查看
题目原文:
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”
题目大意:
给出一个字符串,去掉其中重复的字母,并使得剩下的字符串是字典序最小的。
例如aba,可以去掉两个a中之一,得到的子串是”ab”或”ba”,要取字典序最小的”ab”.
题目分析:
参考discuss中的一个神解法,大致思路是使用两个指针模拟栈,具体细节有待研究。
源码:(language:java)
成绩:
3ms,beats 95.32%,众数7ms,9.99%
cmershen的碎碎念:
度娘上各大博客中比较流行的是这个代码:
大致思路是每次去掉一个字符,取字典序较小的子串继续进行比较,但这个算法虽然是O(n)复杂度,但并不快(40ms+).因此还是想有时间认真研读一下那个3ms的解法。
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”
题目大意:
给出一个字符串,去掉其中重复的字母,并使得剩下的字符串是字典序最小的。
例如aba,可以去掉两个a中之一,得到的子串是”ab”或”ba”,要取字典序最小的”ab”.
题目分析:
参考discuss中的一个神解法,大致思路是使用两个指针模拟栈,具体细节有待研究。
源码:(language:java)
public class Solution { public String removeDuplicateLetters(String s) { /** * First loop: use an array cnt[] to count the number of times * appeared for each letter in s. * * Second loop (Greedy): use a stack, pop() while (!stack.isEmpty() * && (sc = stack.peek()) >= c && cnt[sc] > 0) */ int i, n = s.length(); int[] cnt = new int[128]; boolean[] inRes = new boolean[128]; // whether a char is in res[] char[] res = s.toCharArray(); // simulate a stack for (i = 0; i < n; i++) cnt[res[i]]++; char c, sc; int end = -1; // now cnt[c] means the remaining count of the char c for (i = 0; i < n; i++) { c = res[i]; if (inRes[c]) { cnt[c]--; continue; } while (end >= 0 && (sc = res[end]) >= c && cnt[sc] > 0) { end--; inRes[sc] = false; } res[++end] = c; cnt[c]--; inRes[c] = true; } return String.valueOf(res).substring(0, end + 1); } }
成绩:
3ms,beats 95.32%,众数7ms,9.99%
cmershen的碎碎念:
度娘上各大博客中比较流行的是这个代码:
public class Solution { public String removeDuplicateLetters(String s) { int[] cnt = new int[26]; int pos = 0; // the position for the smallest s[i] for (int i = 0; i < s.length(); i++) cnt[s.charAt(i) - 'a']++; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) < s.charAt(pos)) pos = i; if (--cnt[s.charAt(i) - 'a'] == 0) break; } return s.length() == 0 ? "" : s.charAt(pos) + removeDuplicateLetters(s.substring(pos + 1).replaceAll("" + s.charAt(pos), "")); } }
大致思路是每次去掉一个字符,取字典序较小的子串继续进行比较,但这个算法虽然是O(n)复杂度,但并不快(40ms+).因此还是想有时间认真研读一下那个3ms的解法。
相关文章推荐
- ztree获取当前选中节点子节点id集合的方法(转载)
- 很好用的在线markdown编辑器
- java-对象管理-集合
- C++ limits头文件的用法(numeric_limits)
- Middle-题目97:148. Sort List
- 工资类(改)
- 关于Cocos2d-x的粒子系统
- &类型参数的坑
- box相关css属性
- acm_Humble Numbers
- 贪心法——最优装载问题
- HUSTOJ 2796 && SPOJ1811
- leetcode 217. Contains Duplicate
- 链表-Remove Nth Node From End of List(删除指定的节点)
- Middle-题目96:187. Repeated DNA Sequences
- MYSQL函数
- 第十四周项目—阅读并运行程序,解释程序执行得到的结果(2)
- cocos2d-x案例开发大全 几何跳跃源码解析及交叉编译再现
- 深复制体验
- 初识Webx 1