namespace ConsoleApp10
{
class Program
{
static void Main(string[] args)
{
Solution solution = new Solution();
solution.generateParenthesis(3);
}
}
class Solution
{
public List<string> generateParenthesis (int n)
{
List<string> res = new List<string>();
if(n == 0)
{
return res;
}
dfs("", n, n, res);
return res;
}
private void dfs(string curStr , int left ,int right,List <string> res)
{
if(left == 0 && right == 0)
{
res.Add(curStr);
return;
}
if(left > 0)
{
dfs(curStr + "(", left - 1, right, res);
}
if(right>0 && left < right)
{
dfs(curStr + ")", left, right - 1, res);
}
}
}
}
深度优先遍历解决括号生成问题
猜你喜欢
转载自blog.csdn.net/xy_learning/article/details/103042631
今日推荐
周排行