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 ...
随机推荐
- (转)Linux系统-tcpdump常用抓包命令
序言 单独总结tcpdump抓包的常用命令 主要语法 过滤主机/IP: tcpdump -i eth1 host 172.16.7.206 抓取所有经过网卡1,目的IP为172.16.7.206的网络 ...
- Shiro ini配置
Shiro.ini配置: ini配置文件类似Java中的properties(key = value),不过提供了key/value分类的特性,每个部分的key不重复即可 在eclipse中设置打开方 ...
- Delphi根据方法名调用方法
type TForm1 = class(TForm) public published procedure DoJsCall(str:string); ...
- Hadoop简介与分布式安装
Hadoop的基本概念和分布式安装: Hadoop 简介 Hadoop 是Apache Lucene创始人道格·卡丁(Doug Cutting)创建的,Lucene是一个应用广泛的文本搜索库,Hado ...
- Django -- settings 详解
Django settings详解 1.基础 DJANGO_SETTING_MODULE环境变量:让settings模块被包含到python可以找到的目录下,开发情况下不需要,我们通常会在当前文件夹运 ...
- 学习js第三天小结
1.冒泡排序分析: 例:将数组[9,8,7,6,5,4,3,2,1,0]按照从小打大的顺序进行冒泡排序. 演变过程: 第一趟: 8,7,6,5,4,3,2,1,0,9 比较了9次: 第二趟: 7 ...
- 通过git将本地文件上传到码云的方法
1. 在码云上创建项目 在码云首页顶部,下图所示,右上角头像旁边的加号,鼠标移上去会显示下拉的,点击“新建项目”. 2. 安装Git 下载完成后安装即可,安装过程中没有注意事项,全部默认一直next直 ...
- tp5 post接到的json被转义怎么解决???
$data =input('post.');//用户唯一标识$goods = $data['goods']; $shopcuxiao=$data['shopcuxiao']; $goods=htmls ...
- 外网访问内网的FTP服务器-原理解析
1. 背景简介 最近研究如何在内网搭架FTP服务器,同时要保证外网(公网)能访问的到.终成正果,但走了一些弯路,在此记下,以飨后人. 2. 基础知识 FTP 使用 2 个端口,一个数据端口和一个命令端 ...
- Solr高效利用:Solr实现SQL的查询与统计
1.如何高效使用Solr查询功能 ?2.单个字段分组统计如何实现? 3.IN条件查询有几种方式? 4.多个字段分组统计是否只支持count? Cloudera公司已经推出了基于Hadoop平台的查询统 ...