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 ...
随机推荐
- C语言函数指针实验
上次看Atmel的示例工程,发现人家使用了函数指针的结构体(函数指针结构体).感叹人家的C语言功夫审核,自己费劲还是只能读懂的份.不过,函数指针确实好用.今天就试试这个超牛的东西.Now let's ...
- Linux 搭建Nexus
Linux 安装Maven和nexus代理仓库 1 说明 环境:redhat Enterprise Linux Server5.3-x64. 版本:Maven 3.0.5 ,Nexus-2.5. ...
- php 运行脚本shell
F:\phpStudy\php53\php.exe -f F:\phpStudy\WWW\qh\qh.php /usr/local/php/bin/php -f test.php Usage: php ...
- Asp.Net MVC及Web API框架配置会碰到的几个问题及解决方案
前言 刚开始创建MVC与Web API的混合项目时,碰到好多问题,今天拿出来跟大家一起分享下.有朋友私信我问项目的分层及文件夹结构在我的第一篇博客中没说清楚,那么接下来我就准备从这些文件怎么分文件夹说 ...
- 问题解决_(转载)在VisualStudio 2012上使用MVC3出现错误的解决办法
错误: 找 不到方 法:“System.Collections.Generic.Dictionary`2<System.String,BlockParser> System.Web.Raz ...
- Json格式转换
验证Json格式可以进入 http://json.cn/ json简单说就是javascript中的对象和数组,所以这两种结构就是对象和数组两种结构,通过这两种结构可以表示各种复杂的结构1.对象:对象 ...
- NET映射导致的应用无法访问
应用环境 IIS 数据库 oracle11G 服务器 windwos2003 企业版 周一早上刚到,听到业务部门的人反应,集团的扫码抢系统登陆不上了,没办法进行出入库操作,大批货物在仓库堆积,承运车 ...
- spring @Scheduled注解执行定时任务
以前框架使用quartz框架执行定时调度问题. 这配置太麻烦.每个调度都需要多加在spring的配置中. 能不能减少配置的量从而提高开发效率. 最近看了看spring的 scheduled的使用注解的 ...
- selenium2.0(WebDriver) API
1.1 下载selenium2.0的包 官方download包地址:http://code.google.com/p/selenium/downloads/list 官方User Guide: h ...
- wordpress woodstock主题导入demo xml文件 execution time out
1.已设置php.ini max_execution_time = 240 导入显示设置60 2.wp-config.php 添加 set_time_limit(600); 无效 3. .htacce ...