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的更多相关文章

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

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

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

  3. 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 ...

  4. leetcode 316. Remove Duplicate Letters

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

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

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

  6. 【leetcode】316. Remove Duplicate Letters

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

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

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

  8. 316 Remove Duplicate Letters 去除重复字母

    给定一个仅包含小写字母的字符串,去除重复的字母使得所有字母出现且仅出现一次.你必须保证返回结果是所有可能结果中的以字典排序的最短结果.例如:给定 "bcabc"返回 "a ...

  9. Remove Duplicate Letters

    316. Remove Duplicate Letters Total Accepted: 2367 Total Submissions: 12388 Difficulty: Medium Given ...

随机推荐

  1. 清空SQL Server数据库中所有表数据的方法(转)

    清空SQL Server数据库中所有表数据的方法 其实删除数据库中数据的方法并不复杂,为什么我还要多此一举呢,一是我这里介绍的是删除数据库的所有数据,因为数据之间可能形成相互约束关系,删除操作可能陷入 ...

  2. memcpy和memmove

    memcpy函数 函数原型 void *memcpy(void *dest, const void *src, size_t n); dest:目标地址 src: 起始地址 n: 字节数 头文件 st ...

  3. EASYUI 表单(FORM)用法

    提交表单 $('#addform').form('submit', { url: '/Admin/AdminUser/AddAdminUser', onSubmit: function () { re ...

  4. C#创建DBF自由库 [转]

    先看段代码: string ole_connstring = @"Provider=VFPOLEDB.1;Data Source=D:\;";System.Data.OleDb.O ...

  5. 反转链表,时间复杂度O(n),空间复杂度O(1)

    原理:使用三个指针,p,q指向交换的元素,r指向后续元素 代码如下: class Node{ int data; Node next; Node(int data){ this.data=data; ...

  6. Win8.1安装Visual Studio 2015提示需要KB2919355

    http://www.microsoft.com/zh-cn/download/details.aspx?id=42335 安装说明: 1.若要开始下载,请单击“下载”按钮,然后执行以下操作之一,或者 ...

  7. lighttpd配置

    1.lighttpd.conf server.modules = ( "mod_access", "mod_alias", "mod_compress ...

  8. Installing Intellij IDEA sublime-text-2 on Ubuntu

    he installation on Linux is traditionally more complicated. I wonder why people complain about the l ...

  9. js执行跨域请求

    //js执行跨域请求 var _script = document.createElement('script'); _script.type = "text/javascript" ...

  10. js疑问

    var arr = ['A', 'B', 'C', 'D', 'E', 'F', 'G'];var aCopy = arr.slice();aCopy; // ['A', 'B', 'C', 'D', ...