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"

思路:采用贪心算法,对于给定的字符串S,找到所有结果的最小前缀(即最小的字母),假如有多个最小前缀,则选择最左侧的。

令最终选择的最小前缀的下标为pos,则删掉s[pos]左侧的所有字符,并删掉剩下字符串中所有与s[pos]相等的字符。

C++中将某个特定字符从所有出现的位置删掉可以这样实现:

s.erase(remove(s.begin(), s.end(), theCharacterNeedtoRemove), s.end());

其中remove函数需要使用algorithm库文件。算法复杂度为O(26 * n) = O(n)

 class Solution {
public:
string removeDuplicateLetters(string s) {
vector<int> alp(, -);
for (int i = , n = s.size(); i < n; i++)
alp[(int)(s[i] - 'a')] = i;
int pos = -;
for (int i = , n = s.size(); i < n; i++)
{
int ind = (int)(s[i] - 'a');
if (alp[ind] == -) continue;
if (pos == - || s[pos] > s[i]) pos = i;
if (alp[ind] == i) break;
}
if (s == "" || pos == -) return "";
char cand = s[pos];
s = s.substr(pos + );
s.erase(remove(s.begin(), s.end(), cand), s.end());
return cand + removeDuplicateLetters(s);
}
};

Remove Duplicate Letters -- LeetCode的更多相关文章

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

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

  2. LeetCode Remove Duplicate Letters

    原题链接在这里:https://leetcode.com/problems/remove-duplicate-letters/ 题目: Given a string which contains on ...

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

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

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

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

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

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

  6. leetcode 316. Remove Duplicate Letters

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

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

  8. 【leetcode】316. Remove Duplicate Letters

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

  9. [Swift]LeetCode316. 去除重复字母 | Remove Duplicate Letters

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

随机推荐

  1. django-settings里redis连接与缓存配置

    # Django-redis的缓存配置 CACHES = { "default": { "BACKEND": "django_redis.cache. ...

  2. 微信小程序--背景图片手机无法预览

    目前小程序好像没有支持手机预览背景本地图片,所以将本地图片改为网络图片链接就可以了 background: url("https://..../img/no.png") no-re ...

  3. 软工实践 - 第十九次作业 Alpha 冲刺 (10/10)

    队名:起床一起肝活队 组长博客:https://www.cnblogs.com/dawnduck/p/10046955.htmlz 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 ...

  4. winform 使用Anchor属性进行界面布局

    每个控件的定位方法: 一.使用Anchor: Anchor分为Left.Top.Right.Bottom四个属性. 它们的含义如下: Top——表示控件中与父窗体(或父控件)相关的顶部应该保持固定. ...

  5. 【Android】实验8 SQLite数据库操作2016.5.12

    实验8  SQLite数据库操作 [目的] 设计一个个人通讯录,掌握Android平台下的数据库开发,该个人通讯录主要包括联系人列表和联系人详细信息等界面. [要求] 程序主界面是通讯录的目录显示手机 ...

  6. 如何从List中删除元素

    从List中删除元素,不能通过索引的方式遍历后删除,只能使用迭代器. 错误的实现 错误的实现方法 public class Demo {     public static void main(Str ...

  7. 内部类(inner class)的简单介绍

    本文主要介绍内部类(inner class)的一些基本应用,将从内部类的分类角度,首先对每一个具体内部类进行介绍.主要包括普通的内部类[common inner class].局部内部类[local ...

  8. CSS Sprites技术

    CSS Sprites技术,国内很多人也叫雪碧图,因为sprite麻 (你买一瓶雪碧就看得到大大的sprite字样了) 主要用于将网站的零碎图标的img标签取代,因为每个img标签引用的src就会造成 ...

  9. Binary Indexted Tree 树状数组入门

    感谢http://www.cnblogs.com/xudong-bupt/p/3484080.html 树状数组(BIT)是能够完成下述操作的数据结构: 给定一初始值全为零的数列a1,a2a,a3.. ...

  10. linux之expr命令

    expr命令可以实现数值运算.数值或字符串比较.字符串匹配.字符串提取.字符串长度计算等功能.它还具有几个特殊功能,判断变量或参数是否为整数.是否为空.是否为0等. 先看expr命令的info文档in ...