mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
8af2fddd3b
commit
9a5914f18d
@ -4,6 +4,7 @@
|
|||||||
Problem source: https://leetcode.com/problems/generate-parentheses/
|
Problem source: https://leetcode.com/problems/generate-parentheses/
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
class Solution:
|
class Solution:
|
||||||
def generateParenthesis(self, n):
|
def generateParenthesis(self, n):
|
||||||
valid = []
|
valid = []
|
||||||
@ -15,12 +16,12 @@ class Solution:
|
|||||||
return
|
return
|
||||||
|
|
||||||
if open_count > 0:
|
if open_count > 0:
|
||||||
s += '('
|
s += "("
|
||||||
generate(s, open_count - 1, close_count)
|
generate(s, open_count - 1, close_count)
|
||||||
s = s[:-1]
|
s = s[:-1]
|
||||||
|
|
||||||
if close_count > 0 and open_count < close_count:
|
if close_count > 0 and open_count < close_count:
|
||||||
s += ')'
|
s += ")"
|
||||||
generate(s, open_count, close_count - 1)
|
generate(s, open_count, close_count - 1)
|
||||||
s = s[:-1]
|
s = s[:-1]
|
||||||
|
|
||||||
@ -28,7 +29,7 @@ class Solution:
|
|||||||
raise ValueError("Input value must be non-negative")
|
raise ValueError("Input value must be non-negative")
|
||||||
|
|
||||||
# Start the generation process with an empty string and n opening and closing parentheses
|
# Start the generation process with an empty string and n opening and closing parentheses
|
||||||
generate('', n, n)
|
generate("", n, n)
|
||||||
"""
|
"""
|
||||||
The function employs backtracking to generate all valid combinations of n pairs of parentheses. It maintains two counters, open_count and close_count, representing available open and close parentheses.
|
The function employs backtracking to generate all valid combinations of n pairs of parentheses. It maintains two counters, open_count and close_count, representing available open and close parentheses.
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user