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 ...
随机推荐
- MVC自我学起之MVCMusic开发中遇到问题:musicstore edit方法出错的原因和解决方法
错误提示: 存储区更新.插入或删除语句影响到了意外的行数(0).实体在加载后可能被修改或删除.刷新 ObjectStateManager 项. 解决案: 1.在view中或model中增加隐藏id 1 ...
- textView富文本点击事件
NSDictionary * attDic = [NSDictionary dictionaryWithObjectsAndKeys:RGBCOLOR(31, 132, 204),NSForegrou ...
- IOS通过PushSharp开源框架发送推送
1,首先生成推送证书: openssl x509 -in aps_developer_identity.cer -inform DER -out aps_developer_identity.pem ...
- 创建兼容的XHR对象
function createXHR () {//创建XMLHttpRequest对象 var xhr=null; if(window.XMLHttpRequest){ createXHR=creat ...
- [Netty 1] 初识Netty
1. 简介 最早接触netty是在阅读Zookeeper源码的时候,后来看到Storm的消息传输层也由ZMQ转为Netty,所以决心好好来研究和学习一下netty这个框架. Netty项目地址:htt ...
- php5.5新特性之yield理解
今天,在阅读别人代码时,其中出现了一个陌生的关键字yield,想一探究竟,于是找到:http://php.net/manual/zh/language.generators.overview.php ...
- Windows安装TensorFlow-Docker Installation of TensorFlow on Windows
TensorFlow是Google开发的进行Deep Learning的包,目前只是支持在Linux和OSX上运行.不过这个秋季或许就有支持Windows的版本出现了,那么对于使用Windows的开发 ...
- [转]MySQL 5.6 全局事务 ID(GTID)实现原理(一)
原文作者:淘长源 原文连接:http://qing.blog.sina.com.cn/1757661907/68c3cad333002qhe.html 转载注明以上信息 MySQL 5.6 的新特 ...
- TeXLive安装过程
Linux系统下TeXLive2016安装教程:http://www.linuxidc.com/Linux/2016-08/133913.htm 安装完成后,在当前用户的 ~/.bashrc 中加入如 ...
- C++----练习--整型赋值时的溢出
1.如果所赋的值超出了类型的取值范围.那么只保留最低位 #include<iostream> int main() { ; //unsigned char c = 256; 有无符号都是一 ...