leetcode 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"
题目意思:去掉重复的字符,保持元序列的位置,并使得字典序最小
真的是。。。想了半天也没想到用栈。看了他人的代码,还有用string 直接搞的
class Solution {
public:
string removeDuplicateLetters(string s) {
int n = s.length();
vector<int> cnt();
vector<int> vis();
for (int i = ; i < n; ++i) {
int x = s[i] - 'a';
cnt[x]++;
}
stack<int> st;
for (int i = ; i < n; ++i) {
int x = s[i] - 'a';
cnt[x]--;
if (st.empty()) {
st.push(x);
vis[x] = ;
}
else {
while (!st.empty() && st.top() > x && cnt[st.top()] && !vis[x]) {
vis[st.top()] = ;
st.pop();
}
if (!vis[x]) st.push(x),vis[x] = ;
}
}
string t = "";
while (!st.empty()) {
int y = st.top();
st.pop();
t = char(y + 'a') + t;
}
return t;
}
};
class Solution {
public:
string removeDuplicateLetters(string s) {
vector<int> dict(, );
vector<bool> visited(, false);
for(auto ch : s) dict[ch]++;
string result = "";
/** the key idea is to keep a monotically increasing sequence **/
for(auto c : s) {
dict[c]--;
/** to filter the previously visited elements **/
if(visited[c]) continue;
while(c < result.back() && dict[result.back()]) {
visited[result.back()] = false;
result.pop_back();
}
result += c;
visited[c] = true;
}
return result.substr();
}
};
leetcode 316. Remove Duplicate Letters的更多相关文章
- leetcode@ [316] Remove Duplicate Letters (Stack & Greedy)
https://leetcode.com/problems/remove-duplicate-letters/ Given a string which contains only lowercase ...
- [LeetCode] 316. Remove Duplicate Letters 移除重复字母
Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...
- 贪心:leetcode 870. Advantage Shuffle、134. Gas Station、452. Minimum Number of Arrows to Burst Balloons、316. Remove Duplicate Letters
870. Advantage Shuffle 思路:A数组的最大值大于B的最大值,就拿这个A跟B比较:如果不大于,就拿最小值跟B比较 A可以改变顺序,但B的顺序不能改变,只能通过容器来获得由大到小的顺 ...
- 【LeetCode】316. Remove Duplicate Letters 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【leetcode】316. 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 ...
- 316. Remove Duplicate Letters
Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...
- 316 Remove Duplicate Letters 去除重复字母
给定一个仅包含小写字母的字符串,去除重复的字母使得所有字母出现且仅出现一次.你必须保证返回结果是所有可能结果中的以字典排序的最短结果.例如:给定 "bcabc"返回 "a ...
- LeetCode题目: Remove Duplicate Letters
问题描述 给一个字符串(只包含小写字母),删除重复的字母, 使得每个字母只出现一次.返回的结果必须是字典顺序最小的. 举例:“bcabc" -> "abc", &q ...
随机推荐
- nmon分析与详解
1.命令安装 1.查看liunx版本版本x86_64_14i 目录:cd /nmon/logs/ 版本x86_64_14i [root@localhost u06]# cd / [root@local ...
- 相应缓存设置HttpCacheability 枚举
成员名称 说明 NoCache 设置 Cache-Control: no-cache 标头.如果没有字段名,则指令应用于整个请求,且在满足请求前,共享(代理服务器)缓存必须对原始 Web 服务 ...
- 使用SqlParameter.SqlDbType和SqlParameter.Size时需要注意的地方
1.DbParameter类是SqlParameter和OracleParameter类的父类.DbParameter.Size用来获取或设置列中数据的最大尺寸(只对文本数据有用). 2.数据类型Ch ...
- POJ1061青蛙的约会
Description 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要的事 ...
- NOIP2013D1T3货车运输(最大生成树+倍增lca)
传送门 这道题,先用kruskal求一遍图中的最大生成树. 然后,倍增求lca,求lca的同时求出边权的最小值. #include <cstring> #include <cstdi ...
- Tell me the area---hdu1798 (数学 几何)
http://acm.hdu.edu.cn/showproblem.php?pid=1798 给你两个圆求阴影部分的面积 求出两个扇形的面积减去四边形的面积 扇形的面积是度数(弧度制)*半径的平方 不 ...
- 10-JS的函数学习
<html> <head> <title>js的函数学习</title> <meta charset="UTF-8"/> ...
- 【stl学习笔记】vector
vector是定义于namespace std内的template: namespace std { template<class T, class Allocator = allocator& ...
- Exchanger使用
Exchanger使用
- Linux 常用检测命令
1.uptime [root@smgsim02 ~]# uptime 15:08:15 up 98 days, 4:19, 2 users, load average: 0.07, 0.29, ...