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);
            }
        }
    }
  • 相关阅读:
    搭建一个Flv视频播放服务器
    nginx 搭建http协议拖动播放 FLV 视频播放服务器
    轻轻松松干掉flash右键菜单方法
    在Eclipse中配置tomcat7.0
    js中cookie的使用详细分析
    windows 版Tomcat 7.0的配置
    FMS服务器录制权限的开放NetStream.Record.NoAccess错误解决方法
    WCF入门(四)——会话与实例
    WCF selfhost测试
    WCF 消息队列通信
  • 原文地址:https://www.cnblogs.com/jasminemzy/p/7984466.html
Copyright © 2011-2022 走看看