java 316.删除重复的字母(第1个).java

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 316.删除重复的字母(第1个).java相关的知识,希望对你有一定的参考价值。

public class Solution {
    public String removeDuplicateLetters(String s) {
        if (s == null || s.length() < 2) return s;
        Map<Character, Integer> map = new HashMap<Character, Integer>();
        for (char c : s.toCharArray()) {
            map.put(c, map.containsKey(c) ? map.get(c) + 1 : 1);
        }
        //Stack<Integer> stack = new ArrayDeque<Integer>();
        Set<Character> set = new HashSet<Character>();
        StringBuilder res = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (!set.contains(c)) {
                //while (!stack.isEmpty() && c < stack.peek() && map.get(stack.peek()) > 0) {
                int idx;
                char last;
                while (res.length() > 0 && c < (last = res.charAt(idx = res.length() - 1))  && map.get(last) > 0) {
                    //set.remove(stack.pop());
                    set.remove(last);
                    res.deleteCharAt(idx);
                }
                //stack.push(c);
                res.append(c);
                set.add(c);
            }
            map.put(c, map.get(c) - 1);
        }
        return res.toString();
    }
}
public class Solution {
    public String removeDuplicateLetters(String s) {
        if (s == null || s.length() <= 1) {
            return s;
        }
        // 1. Get all characters' last position
        Map<Character, Integer> lastPos = new HashMap<Character, Integer>();
        for (int i = 0; i < s.length(); i++) {
            lastPos.put(s.charAt(i), i);
        }
        // 2. find the smallest index's letter
        int start = 0;
        StringBuilder sb = new StringBuilder();
        while (lastPos.size() > 0) {
            char minChar = 'z' + 1;
            int minIndex = start;
            int end = findMinIndex(lastPos);
            for (int i = start; i <= end; i++) {
                if (minChar > s.charAt(i) && lastPos.get(s.charAt(i)) != null) {
                    minChar = s.charAt(i);
                    minIndex = i;
                }
            }
            sb.append(minChar);
            start = minIndex + 1;
            lastPos.remove(minChar);
        }
        return sb.toString();
    }
    private int findMinIndex(Map<Character, Integer> lastPos) {
        int minIndex = Integer.MAX_VALUE;
        for (int pos : lastPos.values()) {
            if (minIndex > pos) {
                minIndex = pos;
            }
        }
        return minIndex;
    }
}
public class Solution {
    public String removeDuplicateLetters(String s) {
        if (s == null || s.length() == 0) return s;
        
        int[] letters = new int[26];
        char[] res = new char[26];
        boolean[] inRes = new boolean[26];
        
        for (char c: s.toCharArray()) letters[c - 'a']++;
        
        int j = 0;
        for (char c: s.toCharArray()) {
            letters[c - 'a']--;
            if (inRes[c - 'a']) continue;
            
            while (j >= 1 && res[j-1] > c && letters[res[j-1] - 'a'] > 0) {
                inRes[res[j-1] - 'a'] = false;
                j--;
            }
            
            inRes[c - 'a'] = true;
            res[j] = c;
            j++;
        }
        
        return String.valueOf(res, 0, j);
    }
}

以上是关于java 316.删除重复的字母(第1个).java的主要内容,如果未能解决你的问题,请参考以下文章

java 316.删除重复的字母(第1个).java

java 316.删除重复的字母(第1个).java

java 316.删除重复的字母(第1个).java

java 316.删除重复的字母(第1个).java

LeetCode 316. Remove Duplicate Letters(贪心)

316. 去除重复字母