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的更多相关文章

  1. leetcode@ [316] Remove Duplicate Letters (Stack & Greedy)

    https://leetcode.com/problems/remove-duplicate-letters/ Given a string which contains only lowercase ...

  2. [LeetCode] 316. Remove Duplicate Letters 移除重复字母

    Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...

  3. 贪心: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的顺序不能改变,只能通过容器来获得由大到小的顺 ...

  4. 【LeetCode】316. Remove Duplicate Letters 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  5. 【leetcode】316. Remove Duplicate Letters

    题目如下: Given a string which contains only lowercase letters, remove duplicate letters so that every l ...

  6. 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 ...

  7. 316. Remove Duplicate Letters

    Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...

  8. 316 Remove Duplicate Letters 去除重复字母

    给定一个仅包含小写字母的字符串,去除重复的字母使得所有字母出现且仅出现一次.你必须保证返回结果是所有可能结果中的以字典排序的最短结果.例如:给定 "bcabc"返回 "a ...

  9. LeetCode题目: Remove Duplicate Letters

    问题描述 给一个字符串(只包含小写字母),删除重复的字母, 使得每个字母只出现一次.返回的结果必须是字典顺序最小的. 举例:“bcabc" -> "abc", &q ...

随机推荐

  1. SpringMVC修改功能

    articleList.jsp <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" ...

  2. dataTables中固定表头

    dataTables中固定表头 加入  bAutowidth:false, <style> #dayReveiveMoney_payment_list_table_wrapper .dat ...

  3. C++中的左移、右移运算

    移位运算包含“逻辑移位”(logical shift)和“算术移位”(arithmetic shift). 逻辑移位:移出去的位丢弃,空缺位(vacant bit)用 0 填充. 算术移位:移出去的位 ...

  4. BZOJ2662[BeiJing wc2012]冻结【SPFA】

    “我要成为魔法少女!” “那么,以灵魂为代价,你希望得到什么?” “我要将有关魔法和奇迹的一切,封印于卡片之中„„”        在这个愿望被实现以后的世界里,人们享受着魔法卡片(SpellCard ...

  5. 【思维】2017多校训练七 HDU6121 Build a tree

    http://acm.hdu.edu.cn/showproblem.php?pid=6121 [题意] 询问n个结点的完全k叉树,所有子树结点个数的异或和是多少 [思路] 一棵完全K叉树,对于树的每一 ...

  6. The Grove(poj 3182)

    题意:一个n*m(n,m<=50)的矩阵有一片连着的树林,Bessie要从起始位置出发绕林子一圈再回来,每次只能向横着.竖着或斜着走一步.问最少需多少步才能完成. /* 如果我们用搜索来写的话, ...

  7. [NOIP1999] 提高组 洛谷P1014 Cantor表

    题目描述 现代数学的著名证明之一是Georg Cantor证明了有理数是可枚举的.他是用下面这一张表来证明这一命题的: 1/1 1/2 1/3 1/4 1/5 … 2/1 2/2 2/3 2/4 … ...

  8. jQuery的对象访问函数(get,index,size,each)

    1.get() 元素集合 取得所有匹配的 DOM 元素集合. 这是取得所有匹配元素的一种向后兼容的方式(不同于jQuery对象,而实际上是元素数组). 如果你想要直接操作 DOM 对象而不是 jQue ...

  9. linux 报错:E: Package 'libmemcached' has no installation candidate

    linux 报错:E: Package 'libmemcached' has no installation candidate 网上查资料说是软件安装源没有这个软件,需要添加软件源. 1.备份源列表 ...

  10. Anagrams(hash表)

    Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...