蜗牛慢慢爬 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. ...
随机推荐
- Java ThreadLocal的使用案例
本文以数据库操作Dao为例进行描述ThreadLocal的使用,如下是一个反例: package com.daxin.threadlocal.dao; import java.sql.Connecti ...
- 实例详解:MFC坐标轴实现
需求:MFC坐标轴实现-----最好有步骤啊,刚刚才接触C++和MFC啊.MFC怎样在特定区域建立坐标轴,x轴自适应,y轴有固定范围,最好有网格. 解决思路:VC 内存绘图,不闪屏,具体代码如下: / ...
- css3动画功能介绍
一:过渡动画---Transitions 含义:在css3中,Transitions功能通过将元素的某个属性从一个属性值在指定的时间内平滑过渡到另一个属性值来实现动画功能. Transitions属性 ...
- rabbitmq安装错误集
1.安装依赖yum -y install make gcc gcc-c++ kernel-devel m4 ncurses-devel openssl-devel unixODBC unixODBC- ...
- ssh访问服务器端visdom
在服务器端启动visdompython -m visdom.server在windows端,将服务器的8097端口重定向到windows端(做了映射绑定):ssh -L 8097:127.0.0.1: ...
- 包学会之浅入浅出 Vue.js:开学篇
2016年,乃至接下来整个2017年,如果你要问前端技术框架什么最火,那无疑就是前端三巨头:React.Angular.Vue.没错,什么jQuery,seaJs,gulp等都逐渐脱离了热点.面试的时 ...
- P1550 [USACO08OCT]打井Watering Hole
题目描述 Farmer John has decided to bring water to his N (1 <= N <= 300) pastures which are conven ...
- 关于MSCOCO_text数据集的探索
最近需要做图片中文本识别的项目,然后为了快速验证模型,所以找到了mscoco-text数据集,网站1上是这么说的: 官网是这么说的: 然而,我下下来之后: 1 - 先导入: 2 - 其中key为'im ...
- day71
上节回顾:(模板层) 1 模板之变量---{{ }} -支持数字,字符串,布尔类型,列表,字典---相当于对它进行了打印 -函数--->相当于加括号运行(不能传参数) -对象---& ...
- jqgrid 设置隔行换色
有时,为美观效应,需要设置jqgrid隔行换色.jqgrid提供altRows属性来配置 启动隔行换色:altRows: true,//隔行换色 $("#filterGrid"). ...