问题描述

给一个字符串(只包含小写字母),删除重复的字母,

使得每个字母只出现一次。返回的结果必须是字典顺序最小的。

举例:“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的更多相关文章

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

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

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

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

  3. leetcode 316. Remove Duplicate Letters

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

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

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

  5. LeetCode Remove Duplicate Letters

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

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

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

  7. 【leetcode】316. Remove Duplicate Letters

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

  8. Remove Duplicate Letters -- LeetCode

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

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

随机推荐

  1. 微信 编码要UTF8

    <%@ WebHandler Language="C#" Class="Handler" %> using System; using System ...

  2. 兼容ie7到ie11,edge,chrome,firefox的ajax发送接收post数据代码

    /* * 生成XMLHttpRequest */ function getxhr() { //获取ajax对象 var xhr = null; try { xhr = new XDomainReque ...

  3. python每日一类(3):os和sys

    os与sys模块的官方解释如下: os: This module provides a portable way of using operating system dependent functio ...

  4. hdu 5062(水题)

    Beautiful Palindrome Number Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  5. springBoot Ribbon 负载均衡

    1.依赖引用 <!-- 引入关于 eureka-server的依赖 --> <dependency> <groupId>org.springframework.cl ...

  6. Codeforces Round #446 (Div. 2) B. Wrath【模拟/贪心】

    B. Wrath time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...

  7. linux下使用gcc/g++编译代码时gets函数有错误

    今天在linux中使用个g++编译一个名为myfirst.cpp的代码的时候,出现如下错误 myfirst.cpp: In function ‘int main()’:myfirst.cpp:11:2 ...

  8. unity3d 场景配置文件生成代码

    using UnityEngine; using UnityEditor; using System.IO; using System; using System.Text; using System ...

  9. Android之9图的制作

    .9.PNG确实是标准的PNG格式,只是在最外面一圈额外增加1px的边框,这个1px的边框就是用来定义图片中可扩展的和静态不变的区域.特别说明,left和top边框中交叉部分是可拉伸部分,未选中部分是 ...

  10. UBIFS - UBI File-System

    参考:http://www.linux-mtd.infradead.org/doc/ubifs.html#L_raw_vs_ftl UBIFS - UBI File-System Table of c ...