LeetCode 22. Generate Parentheses
2016-04-26 01:29
519 查看
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
For example, given n = 3, a solution set is:
"((()))", "(()())", "(())()", "()(())", "()()()"
#include <vector> #include <string> #include <iostream> using namespace std; void generateParenthesis(int left, int right, vector<string>& res, string path) { if(left == 0 && right == 0) { res.push_back(path); return; } if(left > 0) generateParenthesis(left-1, right, res, path + '('); if(right > left) generateParenthesis(left, right-1, res, path + ')'); } vector<string> generateParenthesis(int n) { vector<string> res; string path = ""; generateParenthesis(n, n, res, path); return res; } } int main(void) { vector<string> res = generateParenthesis(3); for(int i = 0; i < res.size(); ++i) { cout << res[i] << endl; } }
相关文章推荐
- Unity Shaders and Effects Cookbook (5-1)LitSphere lighting model
- Linux学习日志(二) 用户
- Centos获取软件包
- app线上推广 Andriod版本app 在各大应用市场上线
- C++函数重载原理
- 逆置单链表,C++封装实现,经过测试。
- [Android]APK一键反编译
- 跟我一起学习MySQL技术内幕(第五版):(第二章学习日记11)
- 设计模式之---单例模式(Singleton Design Pattern)
- 一维数组,二维数组,三维数组,多维数组整理
- 4月26日用友面试题__全解析
- 一维数组建模表示二维的棋盘状态
- codeforces#348-D - Little Artem and Dance-规律-模拟
- https://www.kernel-exploits.com/3/
- 数据结构之单向链表
- 南阳师范学院CSDN高校俱乐部运营策划
- Servlet课程0425(七) 到数据库中去验证用户,同时防止SQL注入漏洞
- 第一章 四大组件
- Spark 数据ETL
- 居中显示and自适应布局