316. Remove Duplicate Letters

Total Accepted: 2367 Total Submissions: 12388 Difficulty: Medium

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

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

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

  2. 316. Remove Duplicate Letters

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

  3. Remove Duplicate Letters I & II

    Remove Duplicate Letters I Given a string which contains only lowercase letters, remove duplicate le ...

  4. LeetCode Remove Duplicate Letters

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

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

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

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

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

  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. iOS-OC-基础-NSNumber常用方法

    /*===========================NSNumber数值对象=========================*/ // 将基本数据类型保存为NSNumber 对象类型 NSNu ...

  2. 手算KMP匹配的Next值和Nextval值

    文章作者:姜南(Slyar) 文章来源:Slyar Home (www.slyar.com) 转载请注明,谢谢合作. KMP 算法我们有写好的函数帮我们计算 Next 数组的值和 Nextval 数组 ...

  3. busybox中tftp服务器使用命令

    参数说明:-l 是local的缩写,后跟存在于Client的源文件名,或下载Client后重命名的文件名.-r 是remote的缩写,后跟Server即PC机tftp服务器根目录中的源文件名,或上传S ...

  4. (原)调用jpeglib对图像进行压缩

    网址:http://www.cnblogs.com/darkknightzh/p/4973828.html.未经允许,严禁转载. 参考网站: http://dev.w3.org/Amaya/libjp ...

  5. 解决MySQL中文乱码的问题

    遇到中文乱码问题,首先用status命令检查数据库的配置,如下: 上图会显示数据库配置的各项信息. 还可以用 show create database XXX,来显示创建数据库的时候的编码设置. 一般 ...

  6. JavaScript权威指南阅读笔记3

    第六章 对象 1.首先是先介绍了对象直接量的格式:对象直接量就是1.由若干个名/值对组成的映射表,2名/值对中间由冒号分割,3名值对之间由逗号分割,4整个映射表由花括号括起来.这样就组成了一个对象直接 ...

  7. Win7/Win8 系统下安装Oracle 10g 提示“程序异常终止,发生未知错误”的解决方法

    我的Oracle 10g版本是10.2.0.1.0,(10.1同理)选择高级安装,提示“程序异常终止,发生未知错误”. 1.修改Oracle 10G\database\stage\prereq\db\ ...

  8. sql 数据库优化

    数据库优化: 1. 显示磁盘秘密: DBCC SHOWCONTIG(B2B_ZRate)  清理磁盘密度 DBCC DBREINDEX(B2B_ZRate) 2.

  9. asp.net笔记

    1.  复习 a)         WebForm前后台页面(aspx, aspx.cs)文件在被访问时,会被编译成类,前台类继承于后台类 b)         被访问时,服务器会创建[前台页面类]对 ...

  10. Keil C -WARNING L15: MULTIPLE CALL TO SEGMENT

    1.第一种错误信息 ***WARNING L15: MULTIPLE CALL TO SEGMENT SEGMENT: ?PR?_WRITE_GMVLX1_REG?D_GMVLX1 CALLER1: ...