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/
|
||||
"""
|
||||
|
||||
|
||||
class Solution:
|
||||
def generateParenthesis(self, n):
|
||||
valid = []
|
||||
@ -15,12 +16,12 @@ class Solution:
|
||||
return
|
||||
|
||||
if open_count > 0:
|
||||
s += '('
|
||||
s += "("
|
||||
generate(s, open_count - 1, close_count)
|
||||
s = s[:-1]
|
||||
|
||||
if close_count > 0 and open_count < close_count:
|
||||
s += ')'
|
||||
s += ")"
|
||||
generate(s, open_count, close_count - 1)
|
||||
s = s[:-1]
|
||||
|
||||
@ -28,7 +29,7 @@ class Solution:
|
||||
raise ValueError("Input value must be non-negative")
|
||||
|
||||
# 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.
|
||||
|
||||
@ -61,4 +62,4 @@ try:
|
||||
# Return the valid combinations
|
||||
print(result)
|
||||
except ValueError:
|
||||
print("Invalid input. Please enter a non-negative integer.")
|
||||
print("Invalid input. Please enter a non-negative integer.")
|
||||
|
Loading…
Reference in New Issue
Block a user