[Leetcode] 22. Generate Parentheses
来源:互联网 发布:lca算法建模 编辑:程序博客网 时间:2024/06/14 01:50
Problem:
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:[
“((()))”,
“(()())”,
“(())()”,
“()(())”,
“()()()”
]
Idea:
1. Use a recursive function list all situations.
- For every situation, use a symbol to record the number of ‘(’ on the left side.
- If one ‘(’ is added into the string, the left number of ‘()’ to be added decreases by 1.
- When the function finishes, the symbol should be 0 and the left number should be 0, too.
2. Use Binary Tree. Constructing a Btree for every situation, regard left node as ‘(’ and right ‘)’. After listing all cases, use inorder traversal to all Btrees, which results come from.(But it seems not to be efficient, need some improvement.)
Solution: (for idea No.1)
class Solution(object): res = [] def construct(self,symbol,num,string): if num == 0 and symbol == 0: self.res.append(string) elif symbol == 0: self.construct(symbol+1,num-1,string+"(") elif symbol > 0: self.construct(symbol-1,num,string+")") if num != 0: self.construct(symbol+1,num-1,string+"(") def generateParenthesis(self, n): """ :type n: int :rtype: List[str] """ self.res = [] if n == 1: return ["()"] self.construct(0,n,"") return self.res
Another good solutions from here
1. Here I wrote an actual Python generator. I allow myself to put the yield q at the end of the line because it’s not that bad and because in “real life” I use Python 3 where I just say yield from generate(…).
def generateParenthesis(self, n): def generate(p, left, right): if right >= left >= 0: if not right: yield p for q in generate(p + '(', left-1, right): yield q for q in generate(p + ')', left, right-1): yield q return list(generate('', n, n))
2.Parameter open tells the number of “already opened” parentheses, and I continue the recursion as long as I still have to open parentheses (n > 0) and I haven’t made a mistake yet (open >= 0).
def generateParenthesis(self, n, open=0): if n > 0 <= open: return ['(' + p for p in self.generateParenthesis(n-1, open+1)] + \ [')' + p for p in self.generateParenthesis(n, open-1)] return [')' * open] * (not n)
Ref:
about Python yield
- [LeetCode]22.Generate Parentheses
- LeetCode --- 22. Generate Parentheses
- LeetCode 22.Generate Parentheses
- [Leetcode] 22. Generate Parentheses
- 【LeetCode】22.Generate Parentheses
- [leetcode] 22.Generate Parentheses
- 【leetcode】22. Generate Parentheses
- Leetcode-22.Generate Parentheses
- [leetcode] 22. Generate Parentheses
- 22. Generate Parentheses LeetCode
- leetcode 22. Generate Parentheses
- [LeetCode]22. Generate Parentheses
- leetcode 22. Generate Parentheses
- leetcode 22. Generate Parentheses
- LeetCode 22. Generate Parentheses
- [LeetCode]22. Generate Parentheses
- LeetCode *** 22. Generate Parentheses
- LeetCode 22. Generate Parentheses
- 73.Set Matrix Zeroes
- 算法优化的一个尝试 python
- oracle 11g 初次配置说明
- swift3自定义下拉刷新控件(一)
- 简单工厂模式,工厂方法模式,抽象工厂模式
- [Leetcode] 22. Generate Parentheses
- GUI编程
- [LeetCode] 73. Set Matrix Zeroes
- 69.补间动画和属性动画
- Android 多媒体 通过MediaRecorder+SurfaceView实现拍照,录像
- 74. Search a 2D Matrix
- [学习交流] OC学习笔记之id类型及应用场景
- 网络编程
- gvim 操作