LintCode-生成括号

来源:互联网 发布:linux系统查看ip地址 编辑:程序博客网 时间:2024/06/09 05:36

生成括号

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

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

“((()))”, “(()())”, “(())()”, “()(())”, “()()()”

public class Solution {    /**     * @param n n pairs     * @return All combinations of well-formed parentheses     */public ArrayList<String> generateParenthesis(int n) {        ArrayList<String> result = new ArrayList<String>();        String str = new String();        findPaths(0, 0, n, str, result);        return result;    }    public void findPaths(int n, int m, int sum, String str, List<String> result) {        String str0 = new String();        String str1 = new String();        if (sum - n > 0) {            str0 = str + "(";            this.findPaths(n + 1, m + 1, sum, str0, result);        }        if (m > 0) {            str1 = str + ")";            this.findPaths(n, m - 1, sum, str1, result);        }        if(sum==n&&m==0) result.add(str);    }}
0 0
原创粉丝点击