题目

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. Nginx HTTPS功能部署实践

    本文出处:http://oldboy.blog.51cto.com/2561410/1889346 30.1 文档目的 本文目的提高自己文档的写作能力及排版能力,加强上课所讲的内容得以锻炼也方便自己以 ...

  2. spring引入properties变量报错

    通过properties配置文件配置数据源,代码如下: <bean class="org.springframework.beans.factory.config.PropertyPl ...

  3. Android SDK4/5/6/7,相册、拍照及裁剪功能及遇见的坑

    保存照片和视频到系统相册显示- http://blog.csdn.net/chendong_/article/details/52290329 Android 7.0 之拍照与图片裁剪适配-http: ...

  4. [转]OpenCV2.4.12 开启OpenGL启用三维可视化支持

    OpenCV默认情况下是不支持OpenGL的,如果要使OpenCV支持OpenGL,则需要重编译,具体步骤如下: 注意事项:从The OpenCV Reference ManualOpenCV参考手册 ...

  5. 如何快速找到某个研究领域的所有SCI期刊

    https://www.toutiao.com/a6624332265285485060/?tt_from=dingtalk&utm_campaign=client_share&tim ...

  6. Publisher和Subscriber节点

    一.Publisher节点 /*"ros/ros.h"里面包含了ROS系统内最常用的一些头文件,包含此文件,便可以使用ROS的核心功能.*/#include "ros/r ...

  7. leetcode121—Best Time to Buy and Sell Stock

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  8. gdb中信号

    信号(Signals) 信号是一种软中断,是一种处理异步事件的方法.一般来说,操作系统都支持许多信号.尤其是UNIX,比较重要应用程序一般都会处理信号.UNIX定义了许 多信号,比如SIGINT表示中 ...

  9. 关于NLB的群集操作模式知识 (转载)

    单播:单播模式是指各节点的网络适配器被重新指定了一个虚拟MAC(由02-bf和群集IP地址组成确保此MAC的唯一性).由于所有绑定群集的网络适配器的MAC都相同,所以在单网卡的情况下,各节点之间是不能 ...

  10. C# 16进制与字符串、字节数组之间的转换

    1.请问c#中如何将十进制数的字符串转化成十六进制数的字符串//十进制转二进制 Console.WriteLine("十进制166的二进制表示: "+Convert.ToStrin ...