资讯专栏INFORMATION COLUMN

LeetCode[316] Remove Duplicate Letters

tomorrowwu / 3075人阅读

摘要:复杂度思路用一个每次考虑当前的字符大小和的顶端字符的大小,如果当前字符比较小的话,则可以出顶端的字符,将当前的字符放进中。需要维持了一个判断当前字符在剩余字符串中的出现次数,考虑能否将这个字符从栈中弹出。

LeetCode[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"

ascii array

复杂度
O(N), O(N)

思路
用一个stack,每次考虑当前的字符大小和stack的顶端字符的大小,如果当前字符比较小的话,则可以poll出stack顶端的字符,将当前的字符放进stack中。需要维持了一个array判断当前字符在剩余字符串中的出现次数,考虑能否将这个字符从栈中弹出。

代码

public String removeDuplicateLetters(String s) {
    Stack stack = new Stack<>();
    int[] arr = new int[26];
    boolean[] visited = new boolean[26];
    for(int i = 0; i < s.length(); i ++) {
        arr[s.charAt(i) - "a"] ++;
    }
    //
    for(int i = 0; i < s.length(); i ++) {
        char ch = s.charAt(i);
        arr[ch - "a"] --;
        if(visited[ch - "a"]) continue;
        if(!stack.isEmpty() && ch < stack.peek()) {
            //
            while(!stack.isEmpty() && arr[stack.peek() - "a"] > 0 && ch < stack.peek()) {
                char temp = stack.pop();
                visited[temp - "a"] --;
            }
        }
        visited[ch - "a"] = true;
        stack.push(ch);
    }
    // build the string;
    StringBuilder builder = new StringBuilder();
    while(!stack.isEmpty()) {
        builder.append(stack.pop());
    }
    return builder.reverse().toString();
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/65267.html

相关文章

  • 316. Remove Duplicate Letters

    摘要:以及枚举的做法,因为这题只有个字母,枚举的复杂度是,参考博客还有先把排序,然后从小到大取字母的写法,参考 316. Remove Duplicate Letters 题目链接:https://leetcode.com/problems... 用一个stack来做,stack里面的字母按增序来排,出现top>cur的时候要把top给pop出来,注意一点是如果后面没有top的话,就不能po...

    chunquedong 评论0 收藏0
  • 316. Remove Duplicate Letters and 402. Remove K Di

    摘要:每个字母只留一个,而且保证字典序最小。从前往后扫描,还要往前看一个检出是否删除的,要用解。需要一个数据结构记录是否使用这个字母,可以用。结构也可以用数组加顶点指针模拟。 316 Remove Duplicate Given a string which contains only lowercase letters, remove duplicate letters so that e...

    novo 评论0 收藏0
  • [LeetCode]Remove Duplicate Letters

    摘要:首先要求就是得保证在原来的字符串中存在当前字符的顺序,即要保证每次的字符的次数大于。读字符的过程中,把字符存到里,当发现之前存的字符中比当前字符大而且频率还大于就可以把那个字符出去。基本思想就是在一定的限制条件下出比当前选择差的元素。 Remove Duplicate Letters Given a string which contains only lowercase lette...

    gaosboy 评论0 收藏0
  • [LintCode/LeetCode] Remove Duplicate Letters

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

    wanghui 评论0 收藏0
  • [LeetCode] 609. Find Duplicate File in System

    Problem Given a list of directory info including directory path, and all the files with contents in this directory, you need to find out all the groups of duplicate files in the file system in terms o...

    sf190404 评论0 收藏0

发表评论

0条评论

tomorrowwu

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<