Commit ecd4666
committed
22. Generate Parentheses
```Solution.c /** * Note: The returned array must be malloced, assume caller calls free(). */ void backtrack(char **result, char *current, int open, int close, int n, int *count) { if (open == n && close == n) { current[open + close] = '\0'; result[*count] = (char *)malloc((2 * n + 1) * sizeof(char)); strcpy(result[*count], current); (*count)++; return; } if (open < n) { current[open + close] = '('; backtrack(result, current, open + 1, close, n, count); } if (close < open) { current[open + close] = ')'; backtrack(result, current, open, close + 1, n, count); } } char** generateParenthesis(int n, int* returnSize) { int maxSize = 1; for (int i = 0; i < n; ++i) { maxSize = maxSize * 2 * (2 * i + 1) / (i + 2); } char **result = (char **)malloc(maxSize * sizeof(char *)); char *current = (char *)malloc((2 * n + 1) * sizeof(char)); current[2 * n] = '\0'; int count = 0; backtrack(result, current, 0, 0, n, &count); free(current); *returnSize = count; return result; } ``` ```Solution.cpp class Solution { public: vector<string> generateParenthesis(int n) { vector<string> result; function<void(string, int, int)> backtrack = [&](string curr, int open, int close) { if (curr.size() == n * 2) { result.push_back(curr); return; } if (open < n) backtrack(curr + '(', open + 1, close); if (close < open) backtrack(curr + ')', open, close + 1); }; backtrack("", 0, 0); return result; } }; ``` ```Solution.cs public class Solution { public IList<string> GenerateParenthesis(int n) { IList<string> result = new List<string>(); char[] current = new char[2 * n]; backtrack(result, current, 0, 0, n); return result; } private void backtrack(IList<string> result, char[] current, int open, int close, int n) { if (open == n && close == n) { result.Add(new string(current)); return; } if (open < n) { current[open + close] = '('; backtrack(result, current, open + 1, close, n); } if (close < open) { current[open + close] = ')'; backtrack(result, current, open, close + 1, n); } } } ``` ```Solution.dart class Solution { List<String> generateParenthesis(int n) { List<String> result = []; void backtrack(String current, int open, int close) { if (current.length == n * 2) { result.add(current); return; } if (open < n) { backtrack(current + '(', open + 1, close); } if (close < open) { backtrack(current + ')', open, close + 1); } } backtrack('', 0, 0); return result; } } ``` ```Solution.erl -spec generate_parenthesis(N :: integer()) -> [unicode:unicode_binary()]. generate_parenthesis(N) -> generate(N, N, <<>>). generate(0, 0, Acc) -> [Acc]; generate(Open, Close, Acc) when Open > 0, Open =< Close -> generate(Open - 1, Close, <<Acc/binary, "(">>) ++ (if Close > Open -> generate(Open, Close - 1, <<Acc/binary, ")">>); true -> [] end); generate(Open, Close, Acc) when Close > Open -> generate(Open, Close - 1, <<Acc/binary, ")">>); generate(_, _, _) -> []. ``` ```Solution.ex defmodule Solution do @SPEC generate_parenthesis(n :: integer) :: [String.t] def generate_parenthesis(n) do generate([], "", 0, 0, n) end defp generate(acc, curr, open, close, n) do if String.length(curr) == n * 2 do [curr | acc] else acc = if open < n do generate(acc, curr <> "(", open + 1, close, n) else acc end if close < open do generate(acc, curr <> ")", open, close + 1, n) else acc end end end end ``` ```Solution.go func generateParenthesis(n int) []string { var res []string var backtrack func(s string, left, right int) backtrack = func(s string, left, right int) { if len(s) == 2*n { res = append(res, s) return } if left < n { backtrack(s+"(", left+1, right) } if right < left { backtrack(s+")", left, right+1) } } backtrack("", 0, 0) return res } ``` ```Solution.java class Solution { public List<String> generateParenthesis(int n) { List<String> result = new ArrayList<>(); backtrack(result, "", 0, 0, n); return result; } private void backtrack(List<String> result, String current, int open, int close, int max) { if (current.length() == max * 2) { result.add(current); return; } if (open < max) { backtrack(result, current + "(", open + 1, close, max); } if (close < open) { backtrack(result, current + ")", open, close + 1, max); } } } ``` ```Solution.js /** * @param {number} n * @return {string[]} */ var generateParenthesis = function(n) { var result = []; var backtrack = function(current, open, close) { if (current.length === n * 2) { result.push(current); return; } if (open < n) { backtrack(current + '(', open + 1, close); } if (close < open) { backtrack(current + ')', open, close + 1); } }; backtrack('', 0, 0); return result; }; ``` ```Solution.kt class Solution { fun generateParenthesis(n: Int): List<String> { val result = mutableListOf<String>() fun backtrack(current: String, open: Int, close: Int) { if (current.length == n * 2) { result.add(current) return } if (open < n) { backtrack(current + "(", open + 1, close) } if (close < open) { backtrack(current + ")", open, close + 1) } } backtrack("", 0, 0) return result } } ``` ```Solution.php class Solution { /** * @param Integer $n * @return String[] */ function generateParenthesis($n) { $result = []; $this->backtrack($result, "", 0, 0, $n); return $result; } private function backtrack(&$result, $current, $open, $close, $max) { if (strlen($current) == $max * 2) { $result[] = $current; return; } if ($open < $max) { $this->backtrack($result, $current . "(", $open + 1, $close, $max); } if ($close < $open) { $this->backtrack($result, $current . ")", $open, $close + 1, $max); } } } ``` ```Solution.py class Solution: def generateParenthesis(self, n: int) -> List[str]: result = [] self.backtrack(result, "", 0, 0, n) return result def backtrack(self, result, current, open, close, max): if len(current) == max * 2: result.append(current) return if open < max: self.backtrack(result, current + "(", open + 1, close, max) if close < open: self.backtrack(result, current + ")", open, close + 1, max) ``` ```Solution.rb # @param {Integer} n # @return {String[]} def generate_parenthesis(n) result = [] backtrack(result, "", 0, 0, n) return result end def backtrack(result, current, open, close, max) if current.length == max * 2 result << current return end if open < max backtrack(result, current + "(", open + 1, close, max) end if close < open backtrack(result, current + ")", open, close + 1, max) end end ``` ```Solution.rkt (define/contract (generate-parenthesis n) (-> exact-integer? (listof string?)) (define (loop left right s) (cond [(and (= left 0) (= right 0)) (list s)] [else (append (if (> left 0) (loop (- left 1) right (string-append s "(")) '()) (if (> right left) (loop left (- right 1) (string-append s ")")) '()))])) (loop n n "") ) ``` ```Solution.rs impl Solution { pub fn generate_parenthesis(n: i32) -> Vec<String> { let mut result = Vec::new(); Self::backtrack(&mut result, String::new(), 0, 0, n); result } fn backtrack(result: &mut Vec<String>, current: String, open: i32, close: i32, max: i32) { if current.len() == (max * 2) as usize { result.push(current); return; } if open < max { Self::backtrack(result, format!("{}(", current), open + 1, close, max); } if close < open { Self::backtrack(result, format!("{}{}", current, ")"), open, close + 1, max); } } } ``` ```Solution.scala object Solution { def generateParenthesis(n: Int): List[String] = { def backtrack(s: String, left: Int, right: Int): List[String] = { if (s.length == n * 2) List(s) else { val addLeft = if (left < n) backtrack(s + "(", left + 1, right) else Nil val addRight = if (right < left) backtrack(s + ")", left, right + 1) else Nil addLeft ++ addRight } } backtrack("", 0, 0) } } ``` ```Solution.swift class Solution { func generateParenthesis(_ n: Int) -> [String] { var result = [String]() backtrack(&result, "", 0, 0, n) return result } private func backtrack(_ result: inout [String], _ current: String, _ open: Int, _ close: Int, _ max: Int) { if current.count == max * 2 { result.append(current) return } if open < max { backtrack(&result, current + "(", open + 1, close, max) } if close < open { backtrack(&result, current + ")", open, close + 1, max) } } } ``` ```Solution.ts function generateParenthesis(n: number): string[] { const res: string[] = []; if (n === 0) return res; function generate(openN: number, closeN: number, path: string) { if (path.length === n * 2) { res.push(path); return; } if (openN < n) { generate(openN + 1, closeN, path + "("); } if (closeN < openN) { generate(openN, closeN + 1, path + ")"); } } generate(0, 0, ""); return res; }; ```1 parent 84cf55a commit ecd4666
File tree
19 files changed
+363
-0
lines changed- sol/solution/0001-0100/0022
19 files changed
+363
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
0 commit comments