316. Remove Duplicate Letters
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"
解题思路:
abccdab
第一步:字母去重abcdab
第二步:map中存放{d=3, b=5, c=2, a=4}
第三步:拼接输出
原理:第一个元素一定会出现在0到2之间(因为字符串中最后一次出现c的位置为2,如果0到2之间没有出现一个元素,那么拼接的字符串将不会有c这个字符)
0到2之间一定会出现角标为2处的元素大于1次,并且角标为2处的元素为当前拼接的字符串中最大的。
根据这个原理来控制b与end的值并且选择(b,end]中最小的元素,拼接字符串。
public class Solution {
public String removeDuplicateLetters(String s) {
// 字母去重
Character old=null;
Character newc;
StringBuilder stb = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
newc = s.charAt(i);
if (old!=newc) {
stb.append(newc);
}
old = newc;
}
s = stb.toString();
// 利用map的自动覆盖得到最后出现的字母角标键值对
Map<Character,Integer> map = new HashMap<Character,Integer>();
for (int i = 0; i < s.length(); i++) {
map.put(s.charAt(i), i);
}
int len = map.size();
// 利用StringBuilder来拼接输出
StringBuilder sb = new StringBuilder();
int b=0;
int end = findMinValue(map);
Character val = findKeyByValue(map,end);
while (sb.length()<len) {
Character minc='z'+1;
for (int i = b; i <= end; i++) {
Character cm = s.charAt(i);
if (cm<minc&&cm<=val&&map.containsKey(cm)) {
minc = cm;
b=i+1;
}
}
sb.append(minc+"");
// 从map中删除
map.remove(minc);
if(minc == val){
end = findMinValue(map);
val = findKeyByValue(map,end);
}
}
return sb.toString();
}
public Character findKeyByValue(Map<Character,Integer> map,int val){
for (Map.Entry<Character,Integer> entry: map.entrySet()) {
if (entry.getValue()==val) {
return entry.getKey();
}
}
return null;
}
public int findMinValue(Map<Character,Integer> map){
int minkey = Integer.MAX_VALUE;
for (Integer index : map.values()) {
if (index<minkey) {
minkey = index;
}
}
return minkey;
}
}
此题的关键在于第30行代码处:对每一个将要输出的字母范围进行限制。
316. Remove Duplicate Letters的更多相关文章
- 贪心: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的顺序不能改变,只能通过容器来获得由大到小的顺 ...
- leetcode@ [316] Remove Duplicate Letters (Stack & Greedy)
https://leetcode.com/problems/remove-duplicate-letters/ Given a string which contains only lowercase ...
- 316. Remove Duplicate Letters (accumulate -> count of the difference elements in a vector)
Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...
- 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 移除重复字母
Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...
- 【leetcode】316. Remove Duplicate Letters
题目如下: Given a string which contains only lowercase letters, remove duplicate letters so that every l ...
- 【LeetCode】316. Remove Duplicate Letters 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 316 Remove Duplicate Letters 去除重复字母
给定一个仅包含小写字母的字符串,去除重复的字母使得所有字母出现且仅出现一次.你必须保证返回结果是所有可能结果中的以字典排序的最短结果.例如:给定 "bcabc"返回 "a ...
- Remove Duplicate Letters
316. Remove Duplicate Letters Total Accepted: 2367 Total Submissions: 12388 Difficulty: Medium Given ...
随机推荐
- OAF_开发系列11_实现OAF通过DataBoundValues动态显示表列的左右对齐
20150712 Created By BaoXinjian
- sklearn学习笔记2
Text classifcation with Naïve Bayes In this section we will try to classify newsgroup messages using ...
- Audio 的一些小笔记
1.在做项目的过程中,对于volume 我们有一个volume curve,就是 0~63 step,每个step对应一个dB值,例如0step对应-90dB, 63 step对应0dB.关于这个0d ...
- 安卓 JDK、SDK、ADT 区别
问题一:android软件开发是用java语法,但是为什么开发环境还需要jdk,有android sdk不就可以了吗? 答: 我知道写字要用笔,但为什么还需要笔芯(墨水),有笔杆不就可以了吗? 问题二 ...
- 【OpenCV练习】图片腐蚀
在简单显示出图片之后,这次尝试一下将图片进行腐蚀操作,代码如下. #include <iostream> #include <opencv2/highgui/highgui.hpp& ...
- variably modified 'dist' at file scope|
转自:http://blog.csdn.net/wusuopubupt/article/details/18408227 错误原因: The reason for this warning is th ...
- <select> 标签使用
jQuery获取Select选择的Text和Value: 1. var checkText=jQuery("#select_id").find("option:selec ...
- nodejs在同一台服务器上部署并同时运行两个或以上服务端时,一个服务用户登录后会挤掉另一个用户的问题
问题描述:一台服务器,部署了两个或以上不同的Web服务,服务A的用户在登陆后,服务B的用户也登陆,此时服务A的用户在点击页面时,会返回登陆页面. 问题根源:浏览器保存的session相同,即cooki ...
- 正则表达式test验证的“bug”
在使用正则表达式对某些字符串进行验证时,我们常常会使用到test方法,而该方法也隐藏着一个陷阱,今天就让我们来看一下这个问题. var str = 'hello jack, hello rose'; ...
- MongoDB(七)MongoDb数据结构
首先,向数据库插入一条bjson数据 首先是定义文档,然后使用admin用户名密码登录,进入test数据库,向test数据库中插入此文档("表名称和表中的记录") 插入结果,查看m ...