zoukankan      html  css  js  c++  java
  • Leetcode 0809 括号

      JAVA :

       List<String> reList = new LinkedList<String>();
    
        public List<String> generateParenthesis(int n) {
            search(0, n, n, new StringBuilder());
            return reList;
        }
    
        private void search(int before, int beforeN, int n, StringBuilder sb) {
            if (n == 0) {
                reList.add(sb.toString());
                return;
            }
            int len = sb.length();
            if (beforeN > 0 && before == 0) {
                search(1, beforeN - 1, n, sb.append('('));
                sb.delete(len, len + 1);
            } else {
                search(before - 1, beforeN, n - 1, sb.append(')'));
                sb.delete(len, len + 1);
                if (beforeN > 0) {
                    search(before + 1, beforeN - 1, n, sb.append('('));
                }
                sb.delete(len, len + 1);
            }
        }

      JS:

    var reArr;
    var generateParenthesis = function (n) {
        reArr = [];
        search(0, n, n, '');
        return reArr;
    };
    
    var search = function (before, beforeN, n, re) {
        if (n === 0) {
            reArr.push(re);
            return;
        }
        if (beforeN > 0 && before == 0) {
            search(before + 1, beforeN - 1, n, re + '(');
        } else {
            search(before - 1, beforeN, n - 1, re + ')');
            if (beforeN > 0) {
                search(before + 1, beforeN - 1, n, re + '(');
            }
        }
    }

  • 相关阅读:
    POJ2352
    POJ 2524 并查集
    A POJ1611
    树状dp--B
    spring mvc实现文件上传与下载
    Joda-Time
    JAVA对象与JSON之间的转换
    jackson-databind注解
    JPA注解
    springMVC、spring、jpa、springData整合配置
  • 原文地址:https://www.cnblogs.com/niuyourou/p/13443135.html
Copyright © 2011-2022 走看看