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 ...
随机推荐
- PhotoshopCS6
download: http://www.playnext.cn/photoshop-cs6.html cracker: http://www.playnext.cn/adobe-cs6-crack. ...
- xtu summer individual-4 B - Party All the Time
Party All the Time Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged on HDU. Orig ...
- POJ1703-Find them, Catch them 并查集构造
Find them, Catch them 好久没有做并查集的题,竟然快把并查集忘完了. 题意:大致是有两个监狱,n个 ...
- Flume+kakfa+sparkStream实时处理数据测试
flume:从数据源拉取数据 kafka:主要起到缓冲从flume拉取多了的数据 sparkStream:对数据进行处理 一.flume拉取数据 1.源数据文件读取配置 在flume目录的 ...
- bitset初始化问题
在C++primer上面说,bitset可以用unsigned long来进行初始化,但是上面的例子只是采用了常数如0xffff,而在实际中,当在vs2010中,我采用unsigned long类型的 ...
- django学习之- json序列化
序列化操作 - Errordict - 自定义Encoder - django的模块可以直接序列化 第一种: from django.core import serializers # 通过这个模块对 ...
- HDU 4738 割边
Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Codeforces Manthan, Codefest 18 (rated, Div. 1 + Div. 2) D,E
D. Valid BFS? time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- HUNAN 11567 Escaping (最大流)
http://acm.hunnu.edu.cn/online/?action=problem&type=list&courseid=0&querytext=&pagen ...
- Codeforces 659B Qualifying Contest【模拟,读题】
写这道题题解的目的就是纪念一下半个小时才读懂题...英文一多读一读就溜号... 读题时还时要静下心来... 题目链接: http://codeforces.com/contest/659/proble ...