LeetCode题目: Remove Duplicate Letters
问题描述
给一个字符串(只包含小写字母),删除重复的字母,
使得每个字母只出现一次。返回的结果必须是字典顺序最小的。
举例:“bcabc" -> "abc", "cbacdcbc" -> "acdb"。
提示:stack、greed
解决本题的关键是如何使用贪心策略
下面C++的实现使用的是这样的策略:
令目标字符串为空,每增加一位就遍历a-z,根据条件来判断当前字符是否可以添加到目标字符串。
因为遍历顺序是字典顺序,因此只要符合条件即可判定该字符就是需要添加的字符。
贪心算法的思想就是选择局部最优的,以达到全局最优。而这样的策略符合贪心选择性质。
#include <iostream>
#include <string>
#include <algorithm>
using namespace std; class Solution {
public:
string removeDuplicateLetters(string s) {
if(s.empty())
return "";
int pos1 = ,pos2,i;
char ch;
bool flag;
string s1;
//结束条件
while(pos1 < s.length())
{
//遍历26个字母
for(ch = 'a';ch <= 'z';++ch)
{
flag = true;
//判断当前字符是否已经存在于S1
if(s1.find(ch) != s1.npos)
{
if(ch == 'z')
pos1 ++;
continue;
} //判断当前字符是否在S->pos1之后
if((pos2 = s.find(ch,pos1)) == s.npos)
{
//如果最后一个都没找到
if(ch == 'z')
pos1 ++;
continue;
} //判断当前字符是否符合条件
for(i = pos1;i < pos2;++i)
{
//只要[pos1,pos2)中存在一个不符合的就退出
if(s.find(s[i],i + ) == s.npos && s1.find(s[i]) == s1.npos)
{
flag = false;
break;
}
}
if(flag)
{
s1.push_back(ch);
pos1 = pos2 + ;
break;
}
}
}
return s1;
}
};
LeetCode题目: Remove Duplicate Letters的更多相关文章
- [LeetCode] 316. Remove Duplicate Letters 移除重复字母
Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...
- 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] Remove Duplicate Letters 移除重复字母
Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...
- LeetCode Remove Duplicate Letters
原题链接在这里:https://leetcode.com/problems/remove-duplicate-letters/ 题目: Given a string which contains on ...
- 【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 ...
- Remove Duplicate Letters -- LeetCode
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的顺序不能改变,只能通过容器来获得由大到小的顺 ...
随机推荐
- centos 下文件夹共享
[root@localhost share]# yum install samba -y[root@localhost share]# cp /etc/samba/smb.conf /etc/samb ...
- BeeFramework 系列
http://ilikeido.iteye.com/category/281079 BeeFramework 系列三 MVC篇上 博客分类: Beeframework iphone开发 mvc框架Be ...
- cp2102通过GPIO连接树莓派
此博客不在更新,我的博客新地址:www.liuquanhao.com ----------------------------------------------------------------- ...
- Dreamweaver安装须知
1.断网安装,否则让你登录邮箱什么的,安装完成后退出,先不要运行: 2.将破解的文件直接复制到安装的文件的地方覆盖:(将32文件夹下的amtlib.dll复制到安装完毕的dw_cs6下,覆盖原来的am ...
- Sublime Text 3 使用技巧,插件
一.安装 官网下载最新版安装包,地址自行百度,或者我的网盘 不要安装某些网站提供的安装包*3,原因如下: 1,安装过程捆绑一些不必要的软件 2,测试过程中,某些功能受到限制 快捷键大全 3,一些设置, ...
- (6)sql/puls
host 在sql/puls中使用cmd或linux操作系统的命令
- ZOJ 4009 And Another Data Structure Problem(ZOJ Monthly, March 2018 Problem F,发现循环节 + 线段树 + 永久标记)
题目链接 ZOJ Monthly, March 2018 Problem F 题意很明确 这个模数很奇妙,在$[0, mod)$的所有数满足任意一个数立方$48$次对$mod$取模之后会回到本身. ...
- Lct浅谈
Lct浅谈 1.对lct的认识 首先要知道$lct$是什么.$lct$的全称为$link-cut-tree$.通过全称可以看出,这个数据结构是维护树上的问题,并且是可以支持连边断边操作.$lct$ ...
- Mediocre String Problem (2018南京M,回文+LCP 3×3=9种做法 %%%千年好题 感谢"Grunt"大佬的细心讲解)
layout: post title: Mediocre String Problem (2018南京M,回文+LCP 3×3=9种做法 %%%千年好题 感谢"Grunt"大佬的细 ...
- 理解竞争条件( Race condition)漏洞
这几天一个叫做"Dirty COW"的linux内核竞争条件漏洞蛮火的,相关公司不但给这个漏洞起了个洋气的名字,还给它设计了logo(见下图),首页,Twitter账号以及网店.恰 ...