题目

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]的更多相关文章

  1. 蜗牛慢慢爬 LeetCode 20. Valid Parentheses [Difficulty: Easy]

    题目 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the i ...

  2. 蜗牛慢慢爬 LeetCode 6. ZigZag Conversion [Difficulty: Medium]

    题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...

  3. 蜗牛慢慢爬 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 ...

  4. 蜗牛慢慢爬 LeetCode 36.Valid Sudoku [Difficulty: Medium]

    题目 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  5. 蜗牛慢慢爬 LeetCode 9. Palindrome Number [Difficulty: Easy]

    题目 Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...

  6. 蜗牛慢慢爬 LeetCode 7. Reverse Integer [Difficulty: Easy]

    题目 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have ...

  7. 蜗牛慢慢爬 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 ...

  8. 蜗牛慢慢爬 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 ...

  9. 蜗牛慢慢爬 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. ...

随机推荐

  1. pyspider爬取数据存入redis--1.安装驱动

    首先安装pyredis的驱动 wget https://pypi.python.org/packages/source/r/redis/redis-2.9.1.tar.gz 解压并cd python  ...

  2. [转]VS2013+简单稀疏光束调整库SSBA配置(64位编译)

    有关SSBA库的资源比较少,我是在Github上搜索下载的,具体的GitHub官方下载地址为:SSBA 下载后在SSBA解压文件夹下新建文件夹build. 打开cmake gui,在source co ...

  3. JS 点击元素发ajax请求 打开一个新窗口

    JS 点击元素发ajax请求 打开一个新窗口 经常在项目中会碰到这样的需求,点击某个元素后,需要发ajax请求,请求成功以后,开发需要把链接传给前端(或者说请求成功后打开新窗口),前端需要通过新窗口打 ...

  4. PAT B1045 快速排序 (25 分)

    著名的快速排序算法里有一个经典的划分过程:我们通常采用某种方法取一个元素作为主元,通过交换,把比主元小的元素放到它的左边,比主元大的元素放到它的右边. 给定划分后的 N 个互不相同的正整数的排列,请问 ...

  5. Jmeter—实现识别验证码登录

    在做自动化测试或压力测试时,验证码总是一个问题.在以往的压力测试经历中,测试一般在独立的测试环境中进行,可以放心禁用验证码或使用万能验证码,这个是最实用的.但是,这两天我尝试了一个使用第三方的图形图像 ...

  6. js 函数作为参数+接受任意数量参数

    javascript中的函数是“复合数据类型”,又成为“引用类型”.引用类型的变量指向存储单元中存放的是它们的实际存放地址.函数名是对函数的一种引用.var a=max_num ;a()就可以调用fu ...

  7. C# winform 设置WebBowser 内核版本

    一种是在网页头部 用 <meta http-equiv="X-UA-Compatible" content="IE=edge"> 使用当前浏览器最新 ...

  8. 20155227《网络对抗》Exp6 信息收集与漏洞扫描

    20155227<网络对抗>Exp6 信息收集与漏洞扫描 实践目标 掌握信息搜集的最基础技能与常用工具的使用方法. 基础问题回答 哪些组织负责DNS,IP的管理. 全球根服务器均由美国政府 ...

  9. RocEDU.课程设计2018 第二周进展 博客补交

    本周计划完成的任务 (1).将开发板和平板电脑及其相关配件连通,并和电脑连接. (2).将代码的运行设备从安卓模拟器改为试验箱的平板电脑,平板电脑上实现软件. 本周实际完成情况 (1).计划完成的第一 ...

  10. 20155339 Exp7 网络欺诈防范

    20155339 Exp7 网络欺诈防范 .基础问题回答 (1)通常在什么场景下容易受到DNS spoof攻击 当连接局域网的时候应该最容易被攻击,比如说连接了一些不清楚是什么的WiFi其实是很容易收 ...