设为首页 加入收藏

TOP

Generate Parentheses(C++)
2018-05-24 08:56:44 】 浏览:199
Tags:Generate Parentheses

Givennpairs of parentheses, write a function to generate all combinations of well-formed parentheses.

class Solution {

public:

vector generateParenthesis(int n)

{

vector ret;

findAll(n,n,"",ret);

return ret;

}

void findAll(int left,int right,string out,vector &ret)

{

if(left>right)

return;

if(left==0&&right==0)

return ret.push_back(out);

else

{

if(left>0)

findAll(left-1,right,out+'(',ret);

if(right>0)

findAll(left,right-1,out+')',ret);

}

}

};

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Letter Combinations of a Phone .. 下一篇Remove Nth Node From End of Lis..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目