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 ...
随机推荐
- 高级类特性----final关键字
final 关键字 在Java中声明类.属性和方法时,可使用关键字final来修饰. final标记的变量(成员变量或局部变量)即成为常量,只能赋值一次. final标记的类不能被继承.提高安全性,提 ...
- Ext3.4--TreeGridDemo
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CategoryProper ...
- linux连接sybase数据库-isql
转自:http://blog.knowsky.com/196438.htm 想要linux连接sybase数据库用命令isql: isql [-U login id] [-P password] [- ...
- AJAX同步设置以及请求代码
全局设置ajax同步 更正一点:这个的同步,针对的是ajax请求的返回,而不是ajax-success返回后所有进行处理后才进行下一步.所以,window.location.href转跳这个在执行的时 ...
- LeetCode——Find Minimum in Rotated Sorted Array
Description: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 ...
- codevs 5966 [SDOI2017]硬币游戏
输入描述 Input Description 输入输出数据精度为1e-10 [题解] #include<cstdio> using namespace std; ; char s[N][N ...
- 如何设置Eclipse工作区默认编辑宽度
1)打开Window => Preferences窗口 2)打开Formatter属性页从Java => CodeStyle => Formatter 3) 单击New创建一个自己 ...
- netty之LengthFieldBasedFrameDecoder解码器
官方api:http://netty.io/4.1/api/io/netty/handler/codec/LengthFieldBasedFrameDecoder.html package com.e ...
- 问答项目---登陆账号密码登陆做AJAX异步校验
异步验证管理员帐号方法: /* 异步验证管理员帐号 */ public function checkAccount(){ if(!IS_AJAX){echo "页面不存在";die ...
- 170804、使用Joda-Time优雅的处理日期时间
简介 在Java中处理日期和时间是很常见的需求,基础的工具类就是我们熟悉的Date和Calendar,然而这些工具类的api使用并不是很方便和强大,于是就诞生了Joda-Time这个专门处理日期时间的 ...