zoukankan      html  css  js  c++  java
  • 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"

    链接: http://leetcode.com/problems/remove-duplicate-letters/

    题解:

    String中去除重复字符,使得去重后的String在lexicographical order里最小。这道题目主要参考了Discuss里lixx2100的解法。就是先遍历一遍String,维护一个count[26],存下来每个字符的count,之后再次遍历String,假设position = 0,每次找到字符s.charAt(i)比s.charAt(pos)小的时候,更新position,同时也更新count,当count[i] == 0的时候,这个字符就是我们要找的结果字符串里的第一个字符。之后因为其他字符的count还都 > 1,我们继续在s.substring(position + 1)的子串里递归查找第二个字符,注意要在这个子串里把第一个字符清除掉。  

    虽然time complexity是O(26n) = O(n),但实际运行起来速度比较慢,因为有很多string的操作,而我们前面也遇到过,s.substring其实已经不是O(1)的复杂度,所以会比较慢。 Discuss里另外一位rikimberley的解法就快了很多,是用数组模拟stack,还要好好体会理解。

    Time Complexity - O(n), Space Complexity - O(n)

    public class Solution {
        public String removeDuplicateLetters(String s) {
            if(s == null || s.length() == 0) {
                return s;
            }
            
            int[] count = new int[26];
            char[] res = new char[26];
            int len = s.length();
            for(int i = 0; i < s.length(); i++) {
                count[s.charAt(i) - 'a']++; 
            }
            
            int pos = 0;
            for(int i = 0; i < len; i++) {
                if(s.charAt(i) < s.charAt(pos)) {
                    pos = i;
                }
                count[s.charAt(i) - 'a']--;     
                if(count[s.charAt(i) - 'a'] == 0) {         // found first minimum char
                    break;
                }
            }
            
            String charToRemove = String.valueOf(s.charAt(pos));
            return charToRemove + removeDuplicateLetters(s.substring(pos + 1).replaceAll(charToRemove, ""));
        }
    }

    Reference:

    https://leetcode.com/discuss/73761/a-short-o-n-recursive-greedy-solution

    https://leetcode.com/discuss/74373/java-2ms-two-pointers-solution-or-stack-simulation-beats-72%25

    https://leetcode.com/discuss/73777/easy-to-understand-iterative-java-solution

    https://leetcode.com/discuss/73824/short-16ms-solution-using-stack-which-can-optimized-down-4ms

    https://leetcode.com/discuss/73806/15-ms-java-solution

    https://leetcode.com/discuss/73869/4ms-c-solution-use-return-string-as-a-stack

    https://leetcode.com/discuss/74121/some-python-solutions

    https://leetcode.com/discuss/74084/my-simple-c-o-n-solution-4ms

    https://leetcode.com/discuss/73897/8ms-o-n-java-solution

  • 相关阅读:
    php 下载文件
    thinkphp3.1 发送email
    微擎 plugin 时间插件 图片上传插件不显示 报错 影响下面执行
    Java中基本数据类型的对比记忆
    堆内存设置以及垃圾回收方式
    try--catch--finally中return返回值执行的顺序(区别)
    Java中的值传递和引用传递
    全面总结sizeof的用法(定义、语法、指针变量、数组、结构体、类、联合体、位域位段)
    10进制转换成16进制最简单的方法
    quartz 框架定时任务,使用spring @Scheduled注解执行定时任务
  • 原文地址:https://www.cnblogs.com/yrbbest/p/5068729.html
Copyright © 2011-2022 走看看