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的顺序不能改变,只能通过容器来获得由大到小的顺 ...
随机推荐
- [9018_1963][IOI_1998]Picture
题目描述 N(N<5000) 张矩形的海报,照片和其他同样形状的图片贴在墙上.它们的边都是垂直的或水平的.每个矩形可以部分或者全部覆盖其他矩形.所有的矩形组成的集合的轮廓称为周长.写一个程序计算 ...
- libevent源码分析之信号处理
新看看官方demo的libevent如何使用信号 int called = 0; static void signal_cb(int fd, short event, void *arg) { str ...
- mysql故障(主从复制sql线程不运行)
故障现象: 进入slave服务器,运行: mysql> show slave status\G ....... Relay_Log_File: localhost Relay_Log_Pos: ...
- Openstack ceilometer
https://www.cnblogs.com/liguangsunls/p/6879879.html
- 我在16ASPX下了一个系统是ACCESS和VS2005做的我想把那个连接数据库的'DB_16aspx'的名字改了进不了了可是?
靠,在web.config或者其他配置文件中把数据库连接字符串改称你的新名字不就行了
- (1)IIS
1.选择控制面板-程序和功能 2.点击左侧“打开或关闭Windows功能 3.internet信息服务下的文件夹全勾 4.安装完成后,选择管理工具 5.选择IIS管理器 6.显示如下
- ST表【p1311】 选择客栈
题目描述 丽江河边有 n 家很有特色的客栈,客栈按照其位置顺序从 1 到 n 编号.每家客栈都按照某一种色调进行装饰(总共 k 种,用整数 0~k-1 表示),且每家客栈都设有一家咖啡店,每家咖啡店均 ...
- luogu P1147 连续自然数和
题目描述 对一个给定的自然数M,求出所有的连续的自然数段,这些连续的自然数段中的全部数之和为M. 例子:1998+1999+2000+2001+2002 = 10000,所以从1998到2002的一个 ...
- 1.14(java学习笔记)数组
假如我们需要用到1000个相同类型的数据,肯定不可能创建1000个变量, 这样既不方便,也不直观,也不便于我们使用.这时就需要用到数组. 一.数组的声明与使用 public class Array { ...
- ssh框架整合shiro权限
关于整合shiro,首先在ssh整合的基础上进行组合 1.首先,要导入几个依赖(整合ssh与shiro的依赖): <properties><shiro.version>1.3. ...