[LeetCode] #22 Generate Parentheses
生活随笔
收集整理的這篇文章主要介紹了
[LeetCode] #22 Generate Parentheses
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
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:
"((()))", "(()())", "(())()", "()(())", "()()()"
本題是括號匹配輸出,利用迭代輸出。時間:3ms
代碼如下:
class Solution { public:void unguarded_generate(vector<string> &result, string curr, int m, int n){if (m == 0 && n == 0){result.push_back(curr);}else{if (m != 0){cout << curr << endl;unguarded_generate(result, curr + "(", m - 1, n);}if (m < n && n != 0){cout << curr << endl;unguarded_generate(result, curr + ")", m, n - 1);}}}vector<string> generateParenthesis(int n) {vector<string> ret;if (n > 0){unguarded_generate(ret, string(), n, n);}return ret;} };?
轉(zhuǎn)載于:https://www.cnblogs.com/Scorpio989/p/4545075.html
總結(jié)
以上是生活随笔為你收集整理的[LeetCode] #22 Generate Parentheses的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: UVA 11825 状态压缩DP+子集思
- 下一篇: C语言再学习——分支结构