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的更多相关文章

  1. Remove Duplicate Letters

    316. Remove Duplicate Letters Total Accepted: 2367 Total Submissions: 12388 Difficulty: Medium Given ...

  2. [Swift]LeetCode316. 去除重复字母 | Remove Duplicate Letters

    Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...

  3. [LeetCode] Remove Duplicate Letters 移除重复字母

    Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...

  4. 316. Remove Duplicate Letters

    Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...

  5. LeetCode Remove Duplicate Letters

    原题链接在这里:https://leetcode.com/problems/remove-duplicate-letters/ 题目: Given a string which contains on ...

  6. leetcode@ [316] Remove Duplicate Letters (Stack & Greedy)

    https://leetcode.com/problems/remove-duplicate-letters/ Given a string which contains only lowercase ...

  7. Remove Duplicate Letters(Java 递归与非递归)

    题目介绍: Given a string which contains only lowercase letters, remove duplicate letters so that every l ...

  8. 【lintcode】834. Remove Duplicate Letters

    题目描述: Given a string which contains only lowercase letters, remove duplicate letters so that every l ...

  9. 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 ...

随机推荐

  1. java.lang.ClassNotFoundException与java.lang.NoClassDefFoundError的区别

    java.lang.ClassNotFoundException与java.lang.NoClassDefFoundError的区别   以前一直没有注意过这个问题,前两天机缘巧合上网查了一下,然后自 ...

  2. NGINX将PHP带参数的URL地址重定向二级或多级域名访问

    今天项目中有一个手机站点需要用*.m.domain.com的三级域名访问. 如手机站点的访问网址为m.domain.com,手机下面的会员实际访问地址为index.php?username=$user ...

  3. YII2操作mongodb笔记(转)

    componets配置: 'mongodb' => [ 'class' => '\yii\mongodb\Connection', 'dsn' => 'mongodb://test: ...

  4. iOS-iOS 获取蓝色文件夹图片

    Xcode创建的iOS项目内存在两种文件夹:Group(黄色, 伪文件夹) 和Folder(蓝色, 真文件夹): Group: Folder: Images.xcassets或Group文件夹内的PN ...

  5. primefaces4.0基本教程以及增删改查

    最近试着用了用primefaces4.0,准备写一个基本的增删改查以及分页程序,但在写的过程中发现了很多问题,本想通过百度.谷歌解决,但无奈中文资料非常少,笔者在坑中不停的打滚,终于完成了一个有着基本 ...

  6. 关于yaf 框架的win安装

    http://www.sunqinglin.cn/index.php/archives/329.html PHP windows下yaf框架的安装和配置 2014年10月28日 ⁄ PHP, 编程开发 ...

  7. 装X之读书籍

    .读书又记不住...读过又有什么用...读万卷书 == 读0卷书 ? .额.读书不是记住的,毕竟哪里有这么多过目不忘的天才...要理解书中的内容...理解... .还是要记的,但是要理解的基础上记住. ...

  8. WCF-复合类型使用;传输图片

    一:WCF服务端 IService1.cs中: public interface IService1 { [OperationContract] [WebInvoke(Method = "P ...

  9. 处理dataTable的行和列数据

    DataTable dt = null; foreach (DataRow dr in dt.Rows) { ; j < dr.ItemArray.Length; j++) { tempColu ...

  10. php连接mysql

    一.php连接mysql的函数 1.mysql_connect 作用:连接mysql eg:$con=mysql_connect('localhost','root','123456'); 2.mys ...