lintcode: 生成括号

生成括号

给定 n 对括号,请写一个函数以将其生成新的括号组合,并返回所有组合结果。

样例

给定 n = 3, 可生成的组合如下:

"((()))", "(()())", "(())()", "()(())", "()()()"

解题

参考链接

采用递归树的思想,当左括号数大于右括号数时可以加左或者右括号,否则只能加左括号,当左括号数达到n时,剩下全部添加右括号

public class Solution {
    /**
     * @param n n pairs
     * @return All combinations of well-formed parentheses
     */
    public ArrayList<String> generateParenthesis(int n) {
        // Write your code here
        ArrayList<String> result  = new ArrayList<String>();
        if(n<=0)
            return result;
        int left = 0,right=0;
        String str="";
        generateParenthesis(n,left,right,str,result);
        return result;
    }
    public void generateParenthesis(int n,int left,int right,String str ,ArrayList<String> result){
        if(left == n){
            for(int i=0;i< n- right;i++)
                str+=")";
            result.add(str);
            return;
        }
        generateParenthesis(n, left+1, right,str+"(",result);
        if(left>right){
            generateParenthesis(n, left, right+1,str+")",result);
        }
    }
}
时间: 2024-11-05 12:23:59

lintcode: 生成括号的相关文章

生成括号

生成括号 给定 n 对括号,请写一个函数以将其生成新的括号组合,并返回所有组合结果. 样例 给定 n = 3, 可生成的组合如下: "((()))", "(()())", "(())()", "()(())", "()()()" 嗯..想了半天. 其实可以当作一个先序遍历二叉树的问题. 根节点是'(',左孩子都是'(',右孩子都是')',然后递归先序遍历.i,j分别是'(',')'剩余的数量,如果序列合法,

【LeetCode-面试算法经典-Java实现】【022-Generate Parentheses(生成括号)】

[022-Generate Parentheses(生成括号)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 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: "((()))", "(()())&qu

[CareerCup] 9.6 Generate Parentheses 生成括号

9.6 Implement an algorithm to print all valid (e.g., properly opened and closed) combinations of n-pairs of parentheses.EXAMPLEInput: 3Output: ((())), (()()), (())(), ()(()), ()()() LeetCode上的原题,请参见我之前的博客Generate Parentheses 生成括号. 解法一: class Solution

LintCode刷题——生成括号

427-生成括号 题目:给定 n 对括号,请写一个函数以将其生成新的括号组合,并返回所有组合结果. 样例:给定 n = 3, 可生成的组合如下:"((()))", "(()())", "(())()", "()(())", "()()()" 算法:对于给定的n,定义一个result字符串,当字符串中的'('的数目大于')'的数目,则该字符串中可以拼接'('或')': 否则,result中只能拼接'('.递归

[LintCode] Generate Parentheses 生成括号

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Have you met this question in a real interview? Example Given n = 3, a solution set is: "((()))", "(()())", "(())()", &qu

[LeetCode] Generate Parentheses 生成括号

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: "((()))", "(()())", "(())()", "()(())", "()()()" 在LeetCo

[LeetCode] 22. Generate Parentheses 生成括号

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: "((()))", "(()())", "(())()", "()(())", "()()()" 给一个数字n,

leetCode 22.Generate Parentheses (生成括号) 解题思路和方法

Generate Parentheses 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: "((()))", "(()())", "(())()", "()(())", "

LeetCode 22. 括号生成(Generate Parentheses)

题目描述 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n =3,生成结果为: [ "((()))", "(()())", "(())()", "()(())", "()()()" ] 解题思路 利用回溯的思想递归的生成括号.具体来说记录当前剩余左括号数left,剩余右括号数right,当前的生成括号字符串s,进行如下操作: 若left为0,说明左括