首页 > 其他 > 详细

leetcode22

时间:2017-05-14 11:26:39      阅读:278      评论:0      收藏:0      [点我收藏+]
public class Solution {
    public IList<string> GenerateParenthesis(int n)
        {
            List<string> list = new List<string>();
            backtrack(list, "", 0, 0, n);
            return list;
        }

        private void backtrack(List<String> list, String str, int open, int close, int max)
        {

            if (str.Length == max * 2)
            {
                list.Add(str);
                return;
            }

            if (open < max)
            {
                backtrack(list, str + "(", open + 1, close, max);
            }
            if (close < open)
            {
                backtrack(list, str + ")", open, close + 1, max);
            }
        }
}

https://leetcode.com/problems/generate-parentheses/#/description

leetcode22

原文:http://www.cnblogs.com/asenyang/p/6851664.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!