码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode22

时间:2017-05-14 12:18:04      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:parent   pen   list   rate   targe   title   new   des   public   

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

标签:parent   pen   list   rate   targe   title   new   des   public   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!