Leetcode - Remove Invalid Parentheses

Leetcode - Remove Invalid Parentheses_第1张图片
Screenshot from 2016-03-01 22:59:12.png

My code:

public class Solution {
    public List removeInvalidParentheses(String s) {
        ArrayList ret = new ArrayList();
        if (s == null || s.length() == 0) {
            ret.add("");
            return ret;
        }
        remove(0, 0, new char[]{'(', ')'}, s, ret);
        return ret;
    }
    
    private void remove(int last_i, int last_j, char[] cmp, String s, ArrayList ret) {
        int stack = 0;
        int i = last_i;
        for (; i < s.length(); i++) {
            char curr = s.charAt(i);
            if (curr == cmp[0])
                stack++;
            if (curr == cmp[1])
                stack--;
            if (stack < 0) { // mismatch at index i
                for (int j = last_j; j <= i; j++) {
                    if (s.charAt(j) == cmp[1] && (j == last_j || s.charAt(j - 1) != cmp[1])) {
                        remove(i, j, cmp, s.substring(0, j) + s.substring(j + 1, s.length()), ret);
                    }
                }
                return;
            }
        }
        String rs = new StringBuilder(s).reverse().toString();
        if (cmp[0] == '(')
            remove(0, 0, new char[]{')', '('}, rs, ret);
        else {
            ret.add(rs);
        }
    }
}

这道题目很难。。。看了答案理解了挺长时间才写出来。
dfs + 反转重新扫描。
参考:
https://leetcode.com/discuss/81478/easy-short-concise-and-fast-java-dfs-3-ms-solution

Anyway, Good luck, Richardo!

My code:

public class Solution {
    public List removeInvalidParentheses(String s) {
        List ret = new ArrayList();
        if (s == null || s.length() == 0) {
            ret.add(s);
            return ret;
        }
        
        helper(s, 0, 0, new char[]{'(', ')'}, ret);
        return ret;
    }
    
    private void helper(String s, int last_i, int last_j, char[] pair, List ret) {
        int cnt = 0;
        for (int i = last_i; i < s.length(); i++) {
            if (s.charAt(i) == pair[0]) {
                cnt++;
            }
            if (s.charAt(i) == pair[1]) {
                cnt--;
            }
            if (cnt >= 0) {
                continue;
            }
            for (int j = last_j; j <= i; j++) {
                if (s.charAt(j) == pair[1] && (j == last_j || s.charAt(j - 1) != pair[1])) {
                    helper(s.substring(0, j) + s.substring(j + 1, s.length()), i, j, pair, ret);
                }
            }
            return;
        }
        String r = new StringBuilder(s).reverse().toString();
        if (pair[0] == '(') {
            helper(r, 0, 0, new char[]{')', '('}, ret);
        }
        else {
            ret.add(r);
        }
    }
}

reference:
https://discuss.leetcode.com/topic/34875/easy-short-concise-and-fast-java-dfs-3-ms-solution/2

第二遍做,看答案还是很快就理解了。
这是DFS的做法。

还有一种BFS的做法:
reference:
https://discuss.leetcode.com/topic/28827/share-my-java-bfs-solution/2

但是相比于DFS,这个BFS有太多meaning less calculation 了。
看下就行。记住DFS做法。

Anyway, Good luck, Richardo! -- 10/12/2016

你可能感兴趣的:(Leetcode - Remove Invalid Parentheses)