zoukankan      html  css  js  c++  java
  • leetcode22- Generate Parentheses- medium

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.

    For example, given n = 3, a solution set is:

    [
      "((()))",
      "(()())",
      "(())()",
      "()(())",
      "()()()"
    ]

    1.(更好)DFS.其实就是从0开始要么加左括号,要么加右括号嘛,给你的要求就是加到每一个时刻都不能右括号比左括号多,而且他们的数量都不能比n多。这样去遍历所有可能。

    2. DFS. 先产生左括号可以在什么index出现的序列List<List<Integer>>,然后再把这个转为List<String>的结果。产生序列的要求是,每次加的新序号比上一个大,但跨度又不能太大,否则中间要插入太多个右括号就死了。同时如果加的位置和上一个有间隔,那说明插入了一些右括号了,你下一次DFS的时候可以给你插的右括号,也就是最大允许间隔又小了。(细节,DFS里加结果后记得返回,否则会无线死循环)

    实现1:

    class Solution {
        public List<String> generateParenthesis(int n) {
            List<String> result = new ArrayList<String>();
            if (n <= 0) {
                return result;
            }
            dfs(n, new String(), 0, 0, result);
            return result;
        }
        
        private void dfs (int n, String crt, int left, int right, List<String> result) {
            
            if (left > n || right > n || right > left) {
                return;
            }
            
            if (left == n && right == n) {
                result.add(crt);
            }
            
            dfs(n, crt + '(', left + 1, right, result);
            dfs(n, crt + ')', left, right + 1, result);
            
        }
    }

    实现2:

    class Solution {
        public List<String> generateParenthesis(int n) {
            
            List<String> result = new ArrayList<String>();
            List<List<Integer>> leftIdxs = new ArrayList<List<Integer>>();
            dfs(n, 0, new ArrayList<Integer>(), leftIdxs);
            
            for (List<Integer> leftIdx : leftIdxs) {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < n; i++) {
                    sb.append(')');
                }
                for (int i = 0; i < n; i++) {
                    sb.insert(leftIdx.get(i), "(");
                }
                result.add(sb.toString());
            }
            return result;
        }
        
        private void dfs (int n, int rightAdded, List<Integer> crt, List<List<Integer>> result) {
            if (n == crt.size()) {
                result.add(new ArrayList<Integer>(crt));
                // 千万记得加了就return!不然继续下去会stack overflow。
                return;
            }
            
            int lastDigit = crt.size() == 0 ? -1 : crt.get(crt.size() - 1);
            int rightNeeded = crt.size() - rightAdded;
            for (int i = lastDigit + 1; i < lastDigit + rightNeeded + 2; i++) {
                crt.add(i);
                dfs(n, rightAdded + i - (lastDigit + 1), crt, result);
                crt.remove(crt.size() - 1);
            }
        }
    }
  • 相关阅读:
    常见的排序算法
    322. Coin Change
    C++ STL中的lower_bound,upper_bound使用小结
    滑动窗口题汇总
    1658. Minimum Operations to Reduce X to Zero
    739. Daily Temperatures
    240. Search a 2D Matrix II
    474. Ones and Zeroes
    221. Maximal Square
    javac不是内部或外部命令
  • 原文地址:https://www.cnblogs.com/jasminemzy/p/7984466.html
Copyright © 2011-2022 走看看