22. Generate Parentheses(回溯)
For example, given n = 3, a solution set is:
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]

思路:向string 中插入( 和 ),每插入一个就减1。 那么如何保证这个combination 是正确的呢?
插入数量不超过n
可以插入 ) 的前提是 ( 的数量大于 )
所以就得到了递归的两个条件。
class Solution(object):
def generateParenthesis(self, n):
"""
:type n: int
:rtype: List[str]
"""
res = []
def help(s,left,right):
if(left==0 and right==0):
res.append(s[:])
return
if left>0:
help(s+'(',left-1,right)
if right>left:
help(s+')',left,right-1)
help('',n,n)
return res
22. Generate Parentheses(回溯)的更多相关文章
- 刷题22. Generate Parentheses
一.题目说明 这个题目是22. Generate Parentheses,简单来说,输入一个数字n,输出n对匹配的小括号. 简单考虑了一下,n=0,输出"";n=1,输出" ...
- [Leetcode][Python]22: Generate Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 22: Generate Parentheseshttps://oj.leet ...
- 22. Generate Parentheses(ML)
22. Generate Parentheses . Generate Parentheses Given n pairs of parentheses, write a function to ge ...
- 【LeetCode】22. Generate Parentheses (2 solutions)
Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...
- 22. Generate Parentheses (recursion algorithm)
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 22. Generate Parentheses C++回溯法
把左右括号剩余的次数记录下来,传入回溯函数. 判断是否得到结果的条件就是剩余括号数是否都为零. 注意判断左括号是否剩余时,加上left>0的判断条件!否则会memory limited erro ...
- 【一天一道LeetCode】#22. Generate Parentheses
一天一道LeetCode (一)题目 Given n pairs of parentheses, write a function to generate all combinations of we ...
- 22. Generate Parentheses产生所有匹配括号的方案
[抄题]: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...
- LeetCode OJ 22. Generate Parentheses
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
随机推荐
- js 对象引用传值
1)当变量是一个对象(例如:{}或[]),使用 a = b = {} 这种形式传值的话,会变成会用传值,修改 a 时 b 会相应变化,修改 b 时 a 也一样会相应变化 var a = b = {}; ...
- 关于MCU的烧录,下载与其他接口的比较(二)
单片机应用系统由硬件和软件组成,软件的载体是硬件的程序存储器,程序存储器采用只读存储器,这种存储器在电源关闭后,仍能保存程序,在系统上电后,CPU可取出这些指令重新执行.只读存储器(Read Only ...
- MQTT的学习研究(九)基于HTTP GET MQTT 抓取消息服务端使用
官方参看文档: HTTP GET 接收主题请求协议和响应协议http://publib.boulder.ibm.com/infocenter/wmqv7/v7r0/topic/com.ibm.mq.c ...
- 深入浅出Docker(五):基于Fig搭建开发环境
概述 在搭建开发环境时,我们都希望搭建过程能够简单,并且一劳永逸,其他的同事可以复用已经搭建好的开发环境以节省开发时间.而在搭建开发环境时,我们经常会被复杂的配置以及重复的下载安装所困扰.在Docke ...
- Weinre 远程调试移动端手机web页面
调试场景 1.调试页面在手机上.2.调试工具在PC的chrome3.手机跟pc要在同一个网络环境下,也就是都使用一个wifi 一.安装 Weinre 1.Weinre是基于nodejs实现的,所以使用 ...
- Minix2.0操作系统公用头文件说明
以下头文件均在目录include/下: ansi.h: 用来检测编译器是否遵循标准C,如果是的话,_ANSI就被定义为31415,如果不是的,则_ANSI未定义.通过这个宏来诊测. limits.h: ...
- 【BZOJ5099】[POI2018]Pionek 几何+双指针
[BZOJ5099][POI2018]Pionek Description 在无限大的二维平面的原点(0,0)放置着一个棋子.你有n条可用的移动指令,每条指令可以用一个二维整数向量表示.每条指令最多只 ...
- [USB] Windows USB/DVD Download Tool
此工具为微软官方U盘启动盘制作工具 Windows USB/DVD Download Tool 说明:https://www.microsoft.com/en-us/download/windows- ...
- android麦克风自录自放demo
extends:http://blog.csdn.net/trbbadboy/article/details/7865530 是一个直接播放麦克风采集到的声音线程类: class RecordThre ...
- java的bean和map互转的工具类
import java.beans.BeanInfo;import java.beans.IntrospectionException;import java.beans.Introspector;i ...