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 ...
随机推荐
- Python Paramiko模块与MySQL数据库操作
Paramiko模块批量管理:通过调用ssh协议进行远程机器的批量命令执行. 要使用paramiko模块那就必须先安装这个第三方模块,仅需要在本地上安装相应的软件(python以及PyCrypto), ...
- MongoDB创建数据库和集合命令db.createCollection详解(转)
切换/创建数据库 use yourDB; 当创建一个集合(table)的时候会自动创建当前数据库 完整的命令如下:db.createCollection(name, {capped: <Boo ...
- C#指定日期为一年中的第几周
/// <summary> /// 获取指定时间在为一年中为第几周 /// </summary> /// <param name="dt">指定 ...
- spring 后置处理器BeanFactoryPostProcessor和BeanPostProcessor的用法和区别
主要区别就是: BeanFactoryPostProcessor可以修改BEAN的配置信息而BeanPostProcessor不能,下面举个例子说明 BEAN类: package com.spring ...
- hibernate4中使用Session doWork()方法进行jdbc操作(代码)
Hibernate3.3.2版本中getSession().connection()已被弃用,hibernate4中官方推荐使用Session doWork()方法进行jdbc操作 首先看看Work接 ...
- Linux关于添加硬盘的那些事儿:笔记
添加新硬盘:http://note.youdao.com/share/?id=8cf27602cdce36e1d4160f00e9004b00&type=note 关于添加硬盘的那些事儿: ...
- 【JavaScript】JS_Object跟Function的区别
JS_Object和Function的区别 我们本次的解释,主要通过下图 粗看该图,估计你不一定能看明白.不过接下来让我逐行向你解释. 最左侧:意思是,有两个对象f1和f2,他们是通过new Foo( ...
- Express开发实例(1) —— Hello,world!
Express是NodeJs开发中最常用的基础模块.NodeJs本身有Http模块,但是易用性并不好,因此有人在此基础上开发了Express模块. 什么是express express提供了丰富的路由 ...
- android 读取SQLite android could not open the database in read/write mode错误
由于AndroidManifest.xml文件中uses-permission没有设置权限问题 <uses-permission android:name="android.permi ...
- HDOJ 4750 Count The Pairs
按边长从小到大排序...再逐个加入(就像MST一样)最先联通的点之间最长路径中的最小值就是新加入的边的长.... Count The Pairs Time Limit: 20000/10000 MS ...