LeetCode OJ 22. Generate Parentheses
题目
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:
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]
解答
这题用回溯的方法来做。。。
按如下规则往一个括号组成的字符串中添加左括号或右括号:如果左括号没用完,可以直接添加左括号,或者当右括号没用完且当前字符串中左括号的数量大于右括号的数量,那么可以添加右括号。添加左括号不会导致字符串中的括号不匹配,因为添加的左括号肯定能在之后通过添加右括号进行匹配,又因为已经匹配的左右括号可以直接忽略,那么当右括号没用完且当前字符串中左括号的数量大于右括号的数量时,忽略已经匹配的左右括号,剩下的全是左括号,所以此时添加右括号可以匹配,而当右括号没用完且当前字符串中左括号的数量不大于右括号的数量时,此时添加的右括号无法在当前字符串中找到一个左括号进行匹配,也无法和之后添加的左括号进行匹配。
下面是AC的代码:
class Solution {
public:
vector<string> ans;
void generator(string str, int left, int right){
if(left == 0 && right == 0){
ans.push_back(str);
return ;
}
if(left > 0){
generator(str + '(', left - 1, right);
}
if(right > 0 && left < right){
generator(str + ')', left, right - 1);
}
}
vector<string> generateParenthesis(int n) {
string str = "";
generator(str, n, n);
return ans;
}
};
103
LeetCode OJ 22. Generate Parentheses的更多相关文章
- [Leetcode][Python]22: Generate Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 22: Generate Parentheseshttps://oj.leet ...
- 【一天一道LeetCode】#22. Generate Parentheses
一天一道LeetCode (一)题目 Given n pairs of parentheses, write a function to generate all combinations of we ...
- 【LeetCode】22. Generate Parentheses (2 solutions)
Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...
- 【LeetCode】22. Generate Parentheses (I thought I know Python...)
I thought I know Python... Actually , I know nothing... 这个题真想让人背下来啊,每一句都很帅!!! Given n pairs of paren ...
- 【LeetCode】22. Generate Parentheses 括号生成
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:括号, 括号生成,题解,leetcode, 力扣,Pyt ...
- 【leetcode】22. Generate Parentheses
题目描述: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...
- LeetCode OJ:Generate Parentheses(括号生成)
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- LeetCode:22. Generate Parentheses(Medium)
1. 原题链接 https://leetcode.com/problems/generate-parentheses/description/ 2. 题目要求 给出一个正整数n,请求出由n对合法的圆括 ...
- 22. Generate Parentheses(ML)
22. Generate Parentheses . Generate Parentheses Given n pairs of parentheses, write a function to ge ...
随机推荐
- ArcGIS for android添加图层几何体
GraphicLayer lyr; Map<String,Object> attr = new HashMap<String,Object>(); attr.put(this. ...
- Maven 包含资源文件
Maven打包时,如何包含资源文件(src/main/java | src/main/resources): 参考博客:http://blog.csdn.net/jsflzhong/article/d ...
- C# 委托在线程与UI界面之间的应用
前景:我们在使用线程的时候,经常会想要访问到Form窗体的控件,也就是线程与UI界面交互,但是他们隶属于连个不同的线程,所以是不能直接访问的,这个时候我们就可以通过委托来实现.打个比方,你想要给远方的 ...
- python re模块和collections
re模块下的常用方法 import re ret = re.findall('a', 'eva egon yuan') # 返回所有满足匹配条件的结果,放在列表里 print(ret) #结果 : [ ...
- python利用socket写一个文件上传
1.先将一张图片拖入‘文件上传’的目录下,利用socket把这张图片写到叫‘yuan’的文件中 2.代码: #模拟服务端 import subprocess import os import sock ...
- appium运行时每次默认弹出appiumsetting与unlock重装,关闭这两个步骤的方法
找到appium安装目录,可以在 appium 源码里(C:\Program Files (x86)\Appium\node_modules\appium\lib\devices\android)注释 ...
- jieba gensim 用法
简单的问答已经实现了,那么问题也跟着出现了,我不能确定问题一定是"你叫什么名字",也有可能是"你是谁","你叫啥"之类的,这就引出了人工智能 ...
- str 操作
str 认识字符串(重点, 多) 字符: 单一的文字符号 字符按照固定的顺序连成串 被' 或者" 或者''' 或者"""括起来的内容 索引 编号, 顺序 从0开 ...
- Mac安装compass失败的原因
之前一直用的windows电脑,突然间切换成mac,各种不习惯,刚开始的时候连文件夹都找不到,悲催…… 还好,熟悉了两天之后,基本上也能够操作了. 然后就是安装各种开发软件,由于个人习惯了使用sass ...
- 详解CSS3属性前缀(转)
原文地址 CSS3的属性为什么要带前缀 使用过CSS3属性的同学都知道,CSS3属性都需要带各浏览器的前缀,甚至到现在,依然还有很多属性需要带前缀.这是为什么呢? 我的理解是,浏览器厂商以前就一直在实 ...