Generate Parentheses

来源:互联网 发布:视频加特效软件 编辑:程序博客网 时间:2024/09/21 09:18
public class Solution {    public List<String> generateParenthesis(int n) {        List<String> res = new LinkedList<>();        if (n < 1) {            return res;        }        helper(res, "", n, n);        return res;    }        private void helper(List<String> res, String str, int leftCount, int rightCount) {        if (leftCount == 0 && rightCount == 0) {            res.add(str);            return;        }        if (leftCount > 0) {            helper(res, str + "(", leftCount - 1, rightCount);        }        if (rightCount > 0 && rightCount > leftCount) {            helper(res, str + ")", leftCount, rightCount - 1);        }    }}

0 0
原创粉丝点击