LeetCode 22. Generate Parentheses
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 22. Generate Parentheses相关的知识,希望对你有一定的参考价值。
Problem:
https://leetcode.com/problems/generate-parentheses/
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:
"((()))", "(()())", "(())()", "()(())", "()()()"
Thought:
use backtracking
reference: https://leetcode.com/discuss/104547/c-backtrack-solution-easy
Code C++:
class Solution { public: void helper(vector<string>& solve, int m, int n, string s){ if (m == 0 && n == 0) { solve.push_back(s); return; } if (m > 0) helper(solve, m - 1, n, s + ‘(‘); if (m < n) helper(solve, m, n - 1, s + ‘)‘); } vector<string> generateParenthesis(int n) { vector<string> solution; string s = ""; helper(solution, n, n, s); return solution; } };
以上是关于LeetCode 22. Generate Parentheses的主要内容,如果未能解决你的问题,请参考以下文章
Leetcode 22. Generate Parentheses
leetcode22. Generate Parentheses
leetcode 22 -- Generate Parentheses
[leetcode-22-Generate Parentheses]