Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
24 lines (18 loc) · 687 Bytes

File metadata and controls

24 lines (18 loc) · 687 Bytes
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/usr/bin/env python
'''
Leetcode: Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()"
'''
from __future__ import division
import random
def parentheses_combination(left, right):
if left == 0 and right == 0: yield ''
if left > 0:
for p in parentheses_combination(left-1, right):
yield '('+p
if right > left:
for p in parentheses_combination(left, right-1):
yield ')'+p
# Other ways?
if __name__ == '__main__':
for p in parentheses_combination(4,4): print p
Morty Proxy This is a proxified and sanitized view of the page, visit original site.