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 ...
随机推荐
- 安装ubuntu14.10系统的那些瞎折腾
前段时间自作孽,安装了ubuntu14.04的64位系统,而我的笔记本又是那种老古董,2G的内存所以装好之后各种不稳定,索性这个周末就重装一下吧,本来打算是直接装我以前的那个ubuntu12.04-i ...
- git push和git pull
git push git push如果直接使用,不加repository和refspec,那么首先根据当前branch的branch name,在配置文件中找到branch.branchName.re ...
- [Head First Python]2. python of comment
1- 多行注释 ''' ''' 或 """ """ '''this is the standard way to include a mul ...
- 第二章——第二节 IPC机制的概述和使用
一.Serialiable与Paracle ①.作用 ②.使用 二.Binder与AIDL ①.各自的作用 三.如何使用IPC机制 举例 四.IPC机制的原理 ①.流程图 ②.自己编译自动生成 ...
- https://github.com/aptana/studio3/releases aptana
https://github.com/aptana/studio3/releases aptana
- PHP内置函数getimagesize()的漏洞
今天程序想压缩一些图片,想获取图片的宽高,在网上查了一下哪些函数可以使用,然后看到getimagesize()这个函数.但是当同事看到这个函数,提醒我说这个函数,运营同事禁止使用.心里就很奇怪,就在网 ...
- dojo Tree 添加、删除节点
var tree=this.tree; var store=tree.model.store; if(this.node){ console.log(this.node) var children=t ...
- abiword Related Pages
Application Framework The 'af' directory contains all source code for the cross-platform application ...
- 常用财务软件:用友,金蝶,新中大,速达,管家婆,金算盘,远方,远光,金钥匙,润衡,浪潮,上海博科,易商,任我行,千方百剂,智管,小蜜蜂,SAP,ORACLE,SSA,QAD,MAPICS,JDE。
常用财务软件:用友,金蝶,新中大,速达,管家婆,金算盘,远方,远光,金钥匙,润衡,浪潮,上海博科,易商,任我行,千方百剂,智管,小蜜蜂,SAP,ORACLE,SSA,QAD,MAPICS,JDE. 申 ...
- toolkit,phonetextbox中实现用户按回车键会换行
今天,了解到一个需求,要在输入框中实现:用户按回车键后换行 输入框是toolkit中的phonetextbox 1.首先google了一下,了解到有MultiLine这个属性,但是找寻了一番之后,居然 ...