[LeetCode] 22. Generate Parentheses

1 · Rain Hu · Nov. 10, 2022, 3:44 p.m.
Summary
22. Generate Parentheses Hardness: \(\color{orange}\textsf{Medium}\) Ralated Topics: String、Dynamic Programming、Backtracking 一、題目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example 1: Input: n = 3 Output: ["((()))","(()())","(())()","()(())","()()()"] Example 2: Input: n = 1 Output: ["()"] Constraints: 1 <= n <= 8 二、分析 DFS 演算法是在遍歷「節點」,而回溯法是在遍歷「樹枝」。站在一個節點上,需思考三個問題: 路徑(PATH):已做出的選擇。 選項(OPTION):當前可以做的選擇。 終止條件(TERMINATE):到達決策樹的底層,無法再做其它選擇。...