MCPcopy Create free account
hub / github.com/Blankj/awesome-java-leetcode / Solution

Class Solution

src/com/blankj/medium/_0022/Solution.java:16–53  ·  view source on GitHub ↗

author: Blankj blog : http://blankj.com time : 2018/01/30 desc :

Source from the content-addressed store, hash-verified

14 * </pre>
15 */
16public class Solution {
17// public List<String> generateParenthesis(int n) {
18// List<String> list = new ArrayList<>();
19// helper(list, "", 0, n);
20// return list;
21// }
22//
23// private void helper(List<String> list, String str, int rightNeed, int leftRest) {
24// if (rightNeed == 0 && leftRest == 0) {
25// list.add(str);
26// return;
27// }
28// if (rightNeed > 0) helper(list, str + ")", rightNeed - 1, leftRest);
29// if (leftRest > 0) helper(list, str + "(", rightNeed + 1, leftRest - 1);
30// }
31
32 public List<String> generateParenthesis(int n) {
33 HashMap<Integer, List<String>> hashMap = new HashMap<>();
34 hashMap.put(0, Collections.singletonList(""));
35 for (int i = 1; i <= n; i++) {
36 List<String> list = new ArrayList<>();
37 for (int j = 0; j < i; j++) {
38 for (String fj : hashMap.get(j)) {
39 for (String fi_j_1 : hashMap.get(i - j - 1)) {
40 list.add("(" + fj + ")" + fi_j_1);// calculate f(i)
41 }
42 }
43 }
44 hashMap.put(i, list);
45 }
46 return hashMap.get(n);
47 }
48
49 public static void main(String[] args) {
50 Solution solution = new Solution();
51 System.out.println(solution.generateParenthesis(3));
52 }
53}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected