LeetCode 316. Remove Duplicate Letters(删除重复字母)
2016-04-23 05:51
274 查看
原题网址: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
Return
Given
Return
方法:逐个字母找出。例如要找出第一个字母,首先找到最右边的一个点,使该点右边能够包含全部的字母,则第一个字母必在该点左边(包含该点),寻找方法是从该点向左搜寻,搜寻到一个字母表最靠前的、位置最靠左的字母,则为第一个字母。
另一种实现:
又一个实现:
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) { char[] sa = s.toCharArray(); int[] alphabet = new int[26]; int count = 0; for(int i=0; i<sa.length; i++) { if (alphabet[sa[i]-'a']==0) count ++; alphabet[sa[i]-'a'] ++; } char[] removed = new char[count]; int left = 0; int right = sa.length; int c = 0; Arrays.fill(alphabet, 0); while (c<count) { right --; if (alphabet[sa[right]-'a']==0) c ++; alphabet[sa[right]-'a'] ++; } for(int i=0; i<count; i++) { char ch = sa[right]; int leftmost=right; for(int j=right-1; j>=left; j--) { if (sa[j] <= ch && alphabet[sa[j]-'a'] != 0) { ch = sa[j]; leftmost = j; } } alphabet[ch-'a'] = 0; removed[i] = ch; left = leftmost + 1; while (right<sa.length-1 && alphabet[sa[right]-'a'] != 1) { if (alphabet[sa[right]-'a'] > 1) alphabet[sa[right]-'a'] --; right ++; } } return new String(removed); } }
另一种实现:
public class Solution { public String removeDuplicateLetters(String s) { int[] f = new int[26]; char[] sa = s.toCharArray(); int count = 0; for(int i=sa.length-1; i>=0; i--) { if (f[sa[i]-'a'] == 0) count ++; f[sa[i]-'a'] ++; } char[] unique = new char[count]; Arrays.fill(unique, (char)0xff); int from = 0, to = 0; int pos = 0; for(int i=0; i<count; i++) { while (to<sa.length && (f[sa[to]-'a']!=1)) f[sa[to++]-'a']--; for(int j=from; j<=to; j++) { if (f[sa[j]-'a']>0 && sa[j] < unique[i]) { unique[i] = sa[j]; pos = j; } } while (pos+1<to) f[sa[--to]-'a']++; f[unique[i]-'a'] = 0; from = pos + 1; to = from; } return new String(unique); } }
又一个实现:
public class Solution { public String removeDuplicateLetters(String s) { if (s == null || s.length() == 0) return s; char[] sa = s.toCharArray(); int[] f = new int[26]; int len = 0; for(int i=0; i<sa.length; i++) { if (f[sa[i]-'a'] ++ == 0) len ++; } char[] unique = new char[len]; int from = 0; for(int i=0; i<unique.length; i++) { int right = from; int pos = -1; while (right < sa.length && f[sa[right++]-'a']-- != 1); for(int j=from; j<right; j++) { if (f[sa[j]-'a'] >= 0 && (pos==-1 || sa[j]<sa[pos])) pos = j; } unique[i] = sa[pos]; for(int j=pos; j<right; j++) { f[sa[j]-'a'] ++; } f[sa[pos]-'a'] = 0; from = pos + 1; } return new String(unique); } }
相关文章推荐
- java socket 多线程网络传输多个文件
- 用JAVA捕获屏幕、屏幕录像、播放
- ZOJ(ZJU) 1002 Fire Net(深搜)
- ZOJ(ZJU) 1002 Fire Net(深搜)
- $geoNear
- Leetcode 231. Power of Two & 326. Power of Three & 342. Power of Four
- https://docs.mongodb.org/manual/reference/operator/aggregation/unwind/#examples
- cdh集群安装(提高)
- 284. Peeking Iterator
- mongo
- win10屏幕亮度无法调整的原因
- Quartz.Net State Maintenance And Exception Handling
- 掌握 Dojo 工具包,第 9 部分: Dojo 国际化
- 掌握 Dojo 工具包,第 8 部分: 明日之星 - DojoX
- 130. Surrounded Regions
- 掌握 Dojo 工具包,第 7 部分: Dojo 的扩展
- 掌握 Dojo 工具包,第 6 部分: Dojo Widget 的高级应用
- 掌握 Dojo 工具包,第 5 部分: Dojo 的 UI 组件库 - Dijit
- cdh集群安装(入门)
- NYOJ-15-括号匹配(二)