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的顺序不能改变,只能通过容器来获得由大到小的顺 ...
 
随机推荐
- Broadcom GNSS xxx Geolocaltion Sensor与Windows导航程序的兼容性(转)
			
Broadcom是Windows 8(3G)平板普遍采用的一款GPS传感器, 其windows驱动程序可以提供GNSS接口.GNSS接口提供的数据,说实话确实比普通手机的数据好.在开机.室外.无AGP ...
 - Xcode5 上64位编译 出错No architectures to compile for
			
http://blog.csdn.net/chocolateloveme/article/details/16900999 详细错误信息如下: No architectures to compile ...
 - shell 将字符串分割成数组
			
代码:test.sh #!/bin/bash a="one,two,three,four" #要将$a分割开,可以这样: OLD_IFS="$IFS" IFS= ...
 - Visual Studio跨平台开发(2):Xamarin.iOS基本控制项介绍
			
前言 在上一篇文章中, 我们介绍了Xamarin 以及简单的HelloWorld范例, 这次我们针对iOS的专案目录架构以及基本控制项进行说明. 包含UIButton,UISlider,UISwitc ...
 - AC日记——可能的路径 51nod 1247
			
可能的路径 思路: 看到题目想到gcd: 仔细一看是更相减损: 而gcd是更相减损的优化版: 所以,对于每组数据判断gcd是否相等就好: 来,上代码: #include <cstdio> ...
 - (2).net web api 请求方式与参数
			
一.GET 二.POST 如果方法名前缀不带GET 默认为POST请求方法 1.无参数 2.带一个参数 客户端请求时,名称必须为空,不能是dictCategory.不是空的话,会返回空数据 [ ] 3 ...
 - POJ 3620 Avoid The Lakes【DFS找联通块】
			
Avoid The Lakes Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6826 Accepted: 3637 D ...
 - Cookie和Session在Node.JS中的实践(一)
			
Cookie和Session在Node.JS中的实践(一) Cookie和Session是一个非常有趣的概念,也是一个老生常谈的话题.然而,作者看了许多文章,也翻看了几本书籍,它们对Cookie和Se ...
 - IO 最快的read  和 write
			
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; impo ...
 - SPOJ705 SUBST1 - New Distinct Substrings(后缀数组)
			
给一个字符串求有多少个不相同子串. 每一个子串一定都是某一个后缀的前缀.由此可以推断出总共有(1+n)*n/2个子串,那么下面的任务就是找这些子串中重复的子串. 在后缀数组中后缀都是排完序的,从sa[ ...