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 ...
随机推荐
- 初学linux命令
linux系统的精髓在于它的命令行 早就听说要学习linux系统,就要学习它的命令行(Command Line Interface).说来惭愧,已经使用了linuxmint快两个月了,虽然能够使用一些 ...
- navicat导入mysql数据库sql时报错
今天运维的一个项目需要进入数据库修改下数据,MYSQL的数据库,我先导出了一份数据结构和数据,然后进行修改,改完后发现项目报错了...粗心大意哪改错了,赶紧恢复数据库,结果导入SQL时报错了,当时心凉 ...
- Kafka与Logstash的数据采集
Kafka与Logstash的数据采集 基于Logstash跑通Kafka还是需要注意很多东西,最重要的就是理解Kafka的原理. Logstash工作原理 由于Kafka采用解耦的设计思想,并非原始 ...
- Mysql bigint 类型转为datetime
最近在使用quartz,在mysql中其数据库表中的时间都是使用bigint类型存储的,要想使其查询结果显示为yyyy-mm-dd hh:MM:ss的格式需要使用 from_unixtime()函数, ...
- USB系列之二:读取USB设备的描述符
在前面的文章中,我们已经给出了USB协议的链接地址,从这篇文章起,我们会涉及到许多USB 1.1的内容,我们的指导思想是先从熟悉USB 1.1协议入手,先使用现成的HCD和USBD,直接面对客户端驱动 ...
- (?m)使用实例
示例sql: # User@Host: zjzc_app[zjzc_app] @ [10.22.18.164] Id: 6069153 # Query_time: 153.908486 Lock_ti ...
- bootstrap绿色大气后台模板下载[转]
From:http://www.oschina.net/code/snippet_2364127_48176 1. [图片] 2. [文件] 素材火官网后台模板下载.rar ~ 4MB 下载( ...
- SVN强制填写日志
在F:\Repositories\版本库名\hooks下新建pre-commit.bat 内容如下: @echo off setlocal set SVN_BINDIR="C:\Progra ...
- nginx配置 的话注意几点 1.错误时注意看log 2.天威证书的话,有文档按照其文档一步步配置即可;3每句话的结尾注意千万别丢掉分号
nginx配置 的话注意几点 1.错误时注意看log 2.天威证书的话,有文档按照其文档一步步配置即可:3每句话的结尾注意千万别丢掉分号:4.配置https时 其转发可以转发到http上 pass_ ...
- Python 自动化脚本学习(一)
Python 基础 命令行:在http://www.python.org安装python3,Mac下输入python3进入命令行 整数,浮点数,字符串类型:-1,0.1,'game' 字符串连接和复制 ...