classSolution { public: vector<std::string> generateParenthesis(int n){ vector<string> result; generate("", n, n, result); return result; } private: voidgenerate(string current, int left, int right, vector<string>& result){ if (left == 0 && right == 0) { result.push_back(current); return; } if (left > 0) { generate(current + '(', left - 1, right, result); } if (right > left) { generate(current + ')', left, right - 1, result); } } };
结果
执行用时 : 4 ms, 击败 70.78% 使用 C++ 的用户
内存消耗 : 15.12 MB, 击败 31.77% 使用 C++ 的用户
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
classSolution { public List<String> generateParenthesis(int n) { List<String> result = newArrayList<>(); generate("", n, n, result); return result; } privatevoidgenerate(String current, int left, int right, List<String> result) { if (left == 0 && right == 0) { result.add(current); return; } if (left > 0) { generate(current + '(', left - 1, right, result); } if (right > left) { generate(current + ')', left, right - 1, result); } } }
结果
执行用时 : 1 ms, 击败 71.45% 使用 Java 的用户
内存消耗 : 42.19 MB, 击败 31.97% 使用 Java 的用户
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
classSolution(object): defgenerateParenthesis(self, n): """ :type n: int :rtype: List[str] """ defgenerate(current, left, right, result): if left == 0and right == 0: result.append(current) return if left > 0: generate(current + '(', left - 1, right, result) if right > left: generate(current + ')', left, right - 1, result) result = [] generate("", n, n, result) return result
结果
执行用时 : 20 ms, 击败 61.89% 使用 Python 的用户
内存消耗 : 13.23 MB, 击败 66.19% 使用 Python 的用户
Python3
1 2 3 4 5 6 7 8 9 10 11 12 13
classSolution: defgenerateParenthesis(self, n: int) -> List[str]: defgenerate(current, left, right, result): if left == 0and right == 0: result.append(current) return if left > 0: generate(current + '(', left - 1, right, result) if right > left: generate(current + ')', left, right - 1, result) result = [] generate("", n, n, result) return result
classSolution { funcgenerateParenthesis(_n: Int) -> [String] { var result: [String] = [] backtrack("", 0, 0, n, &result) return result } funcbacktrack(_current: String, _left: Int, _right: Int, _n: Int, _result: inout [String]) { if current.count ==2* n { result.append(current) return } if left < n { backtrack(current +"(", left +1, right, n, &result) } if right < left { backtrack(current +")", left, right +1, n, &result) } } }
结果
执行用时 : 24 ms, 击败 8.82% 使用 Swift 的用户
内存消耗 : 15.61 MB, 击败 7.84% 使用 Swift 的用户
Kotlin
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
classSolution { fungenerateParenthesis(n: Int): List<String> { val result: MutableList<String> = mutableListOf() backtrack("", 0, 0, n, result) return result } privatefunbacktrack(current: String, left: Int, right: Int, n: Int, result: MutableList<String>) { if (current.length == 2 * n) { result.add(current) return } if (left < n) { backtrack("$current(", left + 1, right, n, result) } if (right < left) { backtrack("$current)", left, right + 1, n, result) } } }
结果
执行用时 : 204 ms, 击败 15.38% 使用 Kotlin 的用户
内存消耗 : 36.29 MB, 击败 75.38% 使用 Kotlin 的用户
Dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
classSolution{ List<String> generateParenthesis(int n) { List<String> result = []; _backtrack('', 0, 0, n, result); return result; } void _backtrack(String current, int left, int right, int n, List<String> result) { if (current.length == 2 * n) { result.add(current); return; } if (left < n) { _backtrack('$current(', left + 1, right, n, result); } if (right < left) { _backtrack('$current)', left, right + 1, n, result); } } }
结果
执行用时 : 288 ms, 击败 50.00% 使用 Dart 的用户
内存消耗 : 147.64 MB, 击败 100.00% 使用 Dart 的用户
Go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
funcgenerateParenthesis(n int) []string { var result []string backtrack("", 0, 0, n, &result) return result } funcbacktrack(current string, left int, right int, n int, result *[]string) { iflen(current) == 2*n { *result = append(*result, current) return } if left < n { backtrack(current+"(", left+1, right, n, result) } if right < left { backtrack(current+")", left, right+1, n, result) } }
结果
执行用时 : 0 ms, 击败 100.00% 使用 Go 的用户
内存消耗 : 2.61 MB, 击败 62.59% 使用 Go 的用户
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# @param {Integer} n # @return {String[]} defgenerate_parenthesis(n) result = [] backtrack('', 0, 0, n, result) result end defbacktrack(current, left, right, n, result) if current.length == 2 * n result << current return end if left < n backtrack(current + '(', left + 1, right, n, result) end if right < left backtrack(current + ')', left, right + 1, n, result) end end
结果
执行用时 : 48 ms, 击败 100.00% 使用 Ruby 的用户
内存消耗 : 206.92 MB, 击败 12.50% 使用 Ruby 的用户
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
objectSolution{ defgenerateParenthesis(n: Int): List[String] = { var result: List[String] = List() defbacktrack(current: String, left: Int, right: Int): Unit = { if (current.length == 2 * n) { result = current :: result return } if (left < n) { backtrack(current + "(", left + 1, right) } if (right < left) { backtrack(current + ")", left, right + 1) } } backtrack("", 0, 0) result } }