22. Generate Parentheses
生成匹配的括号
Difficulty: Medium
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:
1 | [ |
Solution
每次可以选择左括号或者右括号,用递归比较简单,注意左括号数小于右括号数
1 | class Solution { |