|
| 1 | +# [Generate Parentheses][title] |
| 2 | + |
| 3 | +## Description |
| 4 | + |
| 5 | +Given *n* pairs of parentheses, write a function to generate all combinations of well-formed parentheses. |
| 6 | + |
| 7 | +For example, given *n* = 3, a solution set is: |
| 8 | + |
| 9 | +``` |
| 10 | +[ |
| 11 | + "((()))", |
| 12 | + "(()())", |
| 13 | + "(())()", |
| 14 | + "()(())", |
| 15 | + "()()()" |
| 16 | +] |
| 17 | +``` |
| 18 | + |
| 19 | +**Tags:** String, Backtracking |
| 20 | + |
| 21 | + |
| 22 | +## 思路 0 |
| 23 | + |
| 24 | +题意是给你 `n` 值,让你找到所有格式正确的圆括号匹配组,题目中已经给出了 `n = 3` 的所有结果。遇到这种问题,第一直觉就是用到递归或者堆栈,我们选取递归来解决,也就是 `helper` 函数的功能,从参数上来看肯定很好理解了,`leftRest` 代表还有几个左括号可以用,`rightNeed` 代表还需要几个右括号才能匹配,初始状态当然是 `rightNeed = 0, leftRest = n`,递归的终止状态就是 `rightNeed == 0 && leftRest == 0`,也就是左右括号都已匹配完毕,然后把 `str` 加入到链表中即可。 |
| 25 | + |
| 26 | +```java |
| 27 | +class Solution { |
| 28 | + public List<String> generateParenthesis(int n) { |
| 29 | + List<String> list = new ArrayList<>(); |
| 30 | + helper(list, "", 0, n); |
| 31 | + return list; |
| 32 | + } |
| 33 | + |
| 34 | + private void helper(List<String> list, String str, int rightNeed, int leftRest) { |
| 35 | + if (rightNeed == 0 && leftRest == 0) { |
| 36 | + list.add(str); |
| 37 | + return; |
| 38 | + } |
| 39 | + if (rightNeed > 0) helper(list, str + ")", rightNeed - 1, leftRest); |
| 40 | + if (leftRest > 0) helper(list, str + "(", rightNeed + 1, leftRest - 1); |
| 41 | + } |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | + |
| 46 | +## 思路 1 |
| 47 | + |
| 48 | +另一种实现方式就是迭代的思想了,我们来找寻其规律如下所示: |
| 49 | + |
| 50 | +``` |
| 51 | +f(0): “” |
| 52 | +
|
| 53 | +f(1): “(“f(0)”)” |
| 54 | +
|
| 55 | +f(2): "(“f(0)”)"f(1), “(“f(1)”)” |
| 56 | +
|
| 57 | +f(3): "(“f(0)”)"f(2), "(“f(1)”)"f(1), “(“f(2)”)” |
| 58 | +... |
| 59 | +``` |
| 60 | + |
| 61 | +可以递推出 `f(n) = "(“f(0)”)"f(n-1) , "(“f(1)”)"f(n-2) "(“f(2)”)"f(n-3) … "(“f(i)”)“f(n-1-i) … “(f(n-1)”)”` |
| 62 | + |
| 63 | +根据如上递推式写出如下代码应该不难了吧。 |
| 64 | + |
| 65 | +```java |
| 66 | +class Solution { |
| 67 | + public List<String> generateParenthesis(int n) { |
| 68 | + HashMap<Integer, List<String>> hashMap = new HashMap<>(); |
| 69 | + hashMap.put(0, Collections.singletonList("")); |
| 70 | + for (int i = 1; i <= n; i++) { |
| 71 | + List<String> list = new ArrayList<>(); |
| 72 | + for (int j = 0; j < i; j++) { |
| 73 | + for (String fj : hashMap.get(j)) { |
| 74 | + for (String fi_j_1 : hashMap.get(i - j - 1)) { |
| 75 | + list.add("(" + fj + ")" + fi_j_1);// calculate f(i) |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + hashMap.put(i, list); |
| 80 | + } |
| 81 | + return hashMap.get(n); |
| 82 | + } |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | + |
| 87 | +## 结语 |
| 88 | + |
| 89 | +如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-java-leetcode][ajl] |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | +[title]: https://leetcode.com/problems/generate-parentheses |
| 94 | +[ajl]: https://github.com/Blankj/awesome-java-leetcode |
0 commit comments