[LeetCode][python3]0022. Generate Parentheses

  1. My first Solution
class Solution:
def generateParenthesis(self, n: int) -> List[str]:
if n==0:
return []
ans=[]
cur_str=""
self.adder(ans,n,n,cur_str)
return ans


def adder(self,ans,l,r,cur_str):
if r==0:
ans.append(cur_str)
return
if l==r:
self.adder(ans,l-1,r,cur_str+"(")
else:
self.adder(ans,l,r-1,cur_str+")")
if l>0:
self.adder(ans,l-1,r,cur_str+"(")
class Solution:
def generateParenthesis(self, n: int) -> List[str]:
res = ["()"]
for i in range(n - 1):
res = set([x[:i] + "()" + x[i:] for x in res for i in range(len(x))])
return res

--

--

--

HI I’m N2I. Now a SWE in Taiwan. Check out more about me in https://nzi2020.blogspot.com/ or contact via email: nayzi9999@gmail.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Creating Terminal Shortcuts via bash_profile Updates on a Mac using Atom Text editor

What is Super Function in Python?

The Developer’s Guide to Lending in Xero

How to setup http password authentication with nginx

Amazon Web Services (AWS) Basics Part 4 — Scalability

Iterating a Python Cipher from Scratch

Suncoast Developers Guild: Week 11

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
N2I

N2I

HI I’m N2I. Now a SWE in Taiwan. Check out more about me in https://nzi2020.blogspot.com/ or contact via email: nayzi9999@gmail.com

More from Medium

SQLite IN OPERATION:

How to Find the Minimal Absolute Sum of Two Elements in an Array (Python Approach)

Crystal Night: Crystal cube, pyramid and prisms shot in studio with colored flashes

LeetCode Patterns Adventure 27 — Lowest Common Ancestor of a Binary Search Tree

Pattern 4: Merge Intervals