22. Generate Parentheses
Problem
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:
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]Related Topics:
String Backtracking
Analysis
方法一:递归。
方法二:非递归。
f(0): ""
f(1): "("f(0)")"
f(2): "("f(0)")"f(1) , "("f(1)")"
f(3): "("f(0)")"f(2) , "("f(1)")"f(1) , "("f(2)")"
f(n): "("f(0)")"f(n-1) , "("f(1)")"f(n-2) , "("f(2)")"f(n-3) ... "("f(n-1)")"Code
递归
非递归
Last updated