Remove Duplicate Letters
316. 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 "bcabc"
Return "abc"
Given "cbacdcbc"
Return "acdb"
class Solution {
public:
string removeDuplicateLetters(string s) {
int size = s.size();
vector<int> vec_cnts(,);
for(int i=; i<size ;++i){
vec_cnts[s[i]-'a']++;
}
string res ;
int res_ch_pos = -;
for(int i=; i<size; ++i){
if(vec_cnts[s[i]-'a'] == ){
continue;
}
if(vec_cnts[s[i]-'a'] != ) {
vec_cnts[s[i]-'a']--;
continue;
}
char ch = s[i] ;
int ch_pos = i;
int j = i-;
while(j > res_ch_pos ){//从后往前找到最前边小于ch的字符,若是没找到小于的字符,则找最前边等于ch的字符
if(s[j] > ch || vec_cnts[s[j]-'a'] == ) {
--j;
continue;
}
if(s[j] < ch){//小于
ch = s[j];
ch_pos = j;
}else{//相等
ch_pos = j;
}
--j;
}
res.push_back(s[ch_pos]);//把找到的字符加入结果集
vec_cnts[s[ch_pos]-'a'] = ;//该字符已经使用,下次不能再使用了
res_ch_pos = ch_pos;//更正当前结果中,最后一个字符所在的位置
if(res_ch_pos < i){//如果结果字符在i之前,下次仍从i开始往前找
i -= ;
}
}
return res;
}
};
/*
"bbbacacca"
*/
Remove Duplicate Letters的更多相关文章
- [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 ...
- Remove Duplicate Letters I & II
Remove Duplicate Letters I Given a string which contains only lowercase letters, remove duplicate le ...
- 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 ...
- [Swift]LeetCode316. 去除重复字母 | Remove Duplicate Letters
Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...
- 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 ...
随机推荐
- ORACLE/MYSQL/DB2等不同数据库取前几条记录
选取数据库中记录的操作是最基础最频繁的,但往往实际应用中不会这么简单,会在选取记录的时候加上一些条件,比如取前几条记录,下面就总结了如何在ORACLE/MYSQL/DB2等一些热门数据库中执行取前几条 ...
- Mybatis Generator最完整配置详解
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration ...
- 生产环境中CentOS7部署NET Core应用程序
NET Core应用程序部署至生产环境中(CentOS7) 阅读目录 环境说明 准备你的ASP.NET Core应用程序 安装CentOS7 安装.NET Core SDK for CentOS7. ...
- C++----练习--while求和
1.完成1+2+3+...+99+100 #include<iostream> int main() { std::cout<<"本程序完成1 + 2 + 3 ... ...
- Hibernate HQL基础 使用参数占位符
在HQL中有两种方法实现使用参数占用符 1.使用? 使用?设置参数占位符,之后通过setString()和setInteger()等方法为其赋值.如: Query query = session.cr ...
- hdu 1466 计算直线的交点数
http://acm.hdu.edu.cn/showproblem.php?pid=1466 N条直线的交点方案数 = c 条直线交叉的交点数与(N-c)条平行线 + c 条直线本身的交点方案 = ( ...
- SqlServer sys.partition_view
--查看partition的四个视图 select * from sys.partition_functions--查看分区函数 select * from sys.partition_paramet ...
- Linux下ld搜索问题:ld: cannot find -l"XX"
ld命令行工具(链接库的一个工具)的搜索路径是-L指定的,库名是-l指定的. 比如: ld -L[dir] -l[mylib] --verbose 以上我用可视化的方法显示ld的搜索路径,其结果是居然 ...
- perl 爬取数据<1>
use LWP::UserAgent; use POSIX; use DBI; $user="root"; $passwd="11111111"; $dbh=& ...
- Word Search II 解答
Question Given a 2D board and a list of words from the dictionary, find all words in the board. Each ...