LeetCode Week13
来源:互联网 发布:淘宝上好看的男装 编辑:程序博客网 时间:2024/06/06 15:46
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:
[ "((()))", "(()())", "(())()", "()(())", "()()()"]
class Solution {public: vector<string> generateParenthesis(int n) { vector<string> rs; string s; genParenthesis(rs,s,n,n); return rs; } void genParenthesis(vector<string>& rs,string &s,int l,int r) { if(l==0) { rs.push_back(s); rs.back().append(r,')'); } s.push_back('('); genParenthesis(rs,s,l-1,r); s.pop_back(); if(l < r) { s.push_back(')'); genParenthesis(rs,s,l,r-1); s.pop_back(); } }};class Solution {public: vector<string> generateParenthesis(int n) { vector<string> rs; if(n>0) genParenthesis(rs,"",0,0,n); return rs; } void genParenthesis(vector<string>& rs,string s,int l,int r,int n) { if(l == n) { rs.push_back(s.append(n-r,')')); return; } genParenthesis(rs,s+"(",l+1,r,n); if(l>r) genParenthesis(rs,s+")",l,r+1,n); }};class Solution {public: vector<string> generateParenthesis(int n) { vector<string> rs; string s=""; if(n>0) genParenthesis(rs,s,0,0,n); return rs; } void genParenthesis(vector<string>& rs,string s,int l,int r,int n) { if(l == n) { rs.push_back(s.append(n-r,')')); return; } s.push_back('('); genParenthesis(rs,s,l+1,r,n); s.pop_back(); if(l>r) { s.push_back(')'); genParenthesis(rs,s,l,r+1,n); s.pop_back(); } }};
阅读全文
0 0
- LeetCode Week13
- LeetCode Week13
- leetcode week13
- LeetCode题解 week13
- LeetCode Week13: Word Break系列
- week13
- week13-leetcode #210-Course-Schedule-II
- week13-leetcode #300-Longest-Increasing-Subsequence
- Leetcode: 40. Combination Sum II(Week13, Medium)
- 算法设计Week13 LeetCode Algorithms Problem #64 Minimum Path Sum
- [LeetCode-Algorithms-40] "Combination Sum II" (2017.11.29-WEEK13)
- [LeetCode-Algorithms-39] "Combination Sum" (2017.11.29-WEEK13)
- LeetCode#671 Second Minimum Node In a Binary Tree (week13)
- Algorithm-week13
- Leetcode 712 Minimum ASCII Delete Sum for Two Strings (算法分析week13)
- 【算法设计作业】week13
- week13---5月27日
- week13---5月28日
- nyoj 635 Oh, my goddess 优先队列+BFS
- nginx php mysql分离架构
- 【数据清洗】数据异常处理之泰尔森回归
- Python:调用NLTK报错LookupError: Recource 'tokenizers/punkt/english.pickle' not found(基于Anaconda)
- 备考PMP第十二天
- LeetCode Week13
- 二叉排序树和哈希表的学习总结
- UML使用——活动图、类图
- JavaSE学习--单例设计模式
- spring的注入参数(bean对象中又有bean属性),beans.xml配置
- Codevs 4244 平衡树练习
- cmd用法
- ImageLoader的使用
- 小米5手机Android运行程序闪退出错解决方法