蜗牛慢慢爬 LeetCode 22. Generate Parentheses [Difficulty: Medium]
题目
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:
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]
翻译
生成所有的括号匹配
Hints
Related Topics: String, Backtracking
利用回溯法
代码
Java
class Solution {
public List<String> generateParenthesis(int n) {
List<String> list = new List<String>();
backtrack("", list, n, n);
return list;
}
public void backtrack(String s, List<String> list, int left, int right){
if(left > right){
return;
}
if(left > 0){
backtrack(s+"(", list, left-1, right);
}
if(right > 0){
backtrack(s+")", list, left, right-1);
}
if(left==0 && right==0){
list.add(s);
return;
}
}
}
Python
class Solution(object):
def generateParenthesis(self, n):
"""
:type n: int
:rtype: List[str]
"""
def backtrack(l, s, left, right, maxlen):
if len(s)==maxlen*2:
l.append(s)
return
if left < maxlen:
backtrack(l, s+'(', left+1, right, maxlen)
if right < left:
backtrack(l, s+')', left, right+1, maxlen)
result = []
backtrack(result, '', 0, 0, n)
return result
蜗牛慢慢爬 LeetCode 22. Generate Parentheses [Difficulty: Medium]的更多相关文章
- 蜗牛慢慢爬 LeetCode 20. Valid Parentheses [Difficulty: Easy]
题目 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the i ...
- 蜗牛慢慢爬 LeetCode 6. ZigZag Conversion [Difficulty: Medium]
题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...
- 蜗牛慢慢爬 LeetCode 16. 3Sum Closest [Difficulty: Medium]
题目 Given an array S of n integers, find three integers in S such that the sum is closest to a given ...
- 蜗牛慢慢爬 LeetCode 36.Valid Sudoku [Difficulty: Medium]
题目 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- 蜗牛慢慢爬 LeetCode 9. Palindrome Number [Difficulty: Easy]
题目 Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...
- 蜗牛慢慢爬 LeetCode 7. Reverse Integer [Difficulty: Easy]
题目 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have ...
- 蜗牛慢慢爬 LeetCode 1.Two Sum [Difficulty: Easy]
题目 Given an array of integers, return indices of the two numbers such that they add up to a specific ...
- 蜗牛慢慢爬 LeetCode 23. Merge k Sorted Lists [Difficulty: Hard]
题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...
- 蜗牛慢慢爬 LeetCode 25. Reverse Nodes in k-Group [Difficulty: Hard]
题目 Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. ...
随机推荐
- 【转】ajax 跨域 headers JavaScript ajax 跨域请求 +设置headers 实践
解决跨域调用服务并设置headers 主要的解决方法需要通过服务器端设置响应头.正确响应options请求,正确设置 JavaScript端需要设置的headers信息 方能实现. 此处手札 供后人参 ...
- Array对象的方法
* Array.isArray(对象)---->判断这个对象是不是数组 * instanceof关键字 * .concat(数组,数组,数组,...) 组合一个新的数组 * .every(函数) ...
- mac apache的使用
因为apache的安装目录/private/etc是默认隐藏的,所以我们需要通过文件夹前往/命令行的方法去找. 或命令行中输入: userdeMBP:~ user$ open /etc 然后都会打开相 ...
- 由于没有公钥,无法验证下列签名: NO_PUBKEY 54422A4B98AB5139
gpg --keyserver pgpkeys.mit.edu --recv-key 54422A4B98AB5139 gpg -a --export 54422A4B98AB5139 | sudo ...
- APR Recipe
1, pt fix 后生成的 icctcl,回到 icc source 后,如果需要eco_route,建议先关掉 timing_driven,因为 icc 与 pt 看到的 timing 情况一般 ...
- shell杂记
(本文将持续更新)从2015年9月25日开始正式学习linux类的东西. 书籍入门:UNIX.Shell编程24学时教程(中文版).Linux与UNIX Shell编程指南.shell十三问.LINU ...
- 3D Touch开发技巧的笔记
iPhone6s以及iPhone6s plus搭载iOS9,有一个新功能叫做3D Touch,这个功能有很大的用处,关键是要会用,这给交互方式又多了一个新的选择和思考,比如说游戏中的额外控制选项.绘图 ...
- Usaco2012-2013 金组 题解 (暂缺Hill walk以及Figue eight)
https://files.cnblogs.com/files/Winniechen/usaco2012-2013.pdf 做的不是很好,还请见谅! 如果有什么疑问,可以QQ上找我. QQ号:1967 ...
- 64位RHEL5系统上运行yum出现"This system is not registered with RHN”的解决方法
在红帽EL5上运行yum,提示“This system is not registered with RHN”,意思是没有在官网上注册,不能下载RH的软件包,替代方案是采用centos源. 1.卸载r ...
- 20155233 Exp1 PC平台逆向破解(5)M
Exp1 PC平台逆向破解(5)M 实践一 手工修改可执行文件,改变程序执行流程,直接跳转到getShell函数. 步骤 1.cp pwn1 pwn20155233拷贝pwn1文件,命名为pwn201 ...