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 ...
随机推荐
- MySQL 之mydumper安装详解
方法一: 安装依赖包: 1 yum install glib2-devel mysql-devel zlib-devel pcre-devel openssl-devel cmake make 下载二 ...
- linux 高级路由
1. 什么是高级路由? 是把信息从源穿过网络到达目的地的行为. 有两个动作:确定最佳路径,传输信息 确定最佳路径:手工指定,自动学习. 传输信息:隧道传输,流量整形 高级路由(策略路由)是根据一定的需 ...
- C# Microsoft.Office不存在空间名称Interop和Excel
在实际开发过程中,我们经常会对Excel表进行操作.相信大家都都已经很熟悉C#操作Excel的步骤:添加引用->COM->Microsoft Office Excel 11 Object. ...
- 开关Windows休眠功能
在windows休眠的时候会把内存里的数据缓存到硬盘的C:/Hiberfil.sys文件,万一断电能够从中恢复状态,然而这对SSD硬盘损耗很大,如果没必要还是关了吧: 关闭: powercfg -h ...
- lua 安装
1:下载安装 curl -R -O http://www.lua.org/ftp/lua-5.3.0.tar.gz tar zxf lua-5.3.0.tar.gz cd lua-5.3.0 make ...
- pthread mutex
pthead_mutex_t mutex; 1:create: pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t ...
- python re模块和collections
re模块下的常用方法 import re ret = re.findall('a', 'eva egon yuan') # 返回所有满足匹配条件的结果,放在列表里 print(ret) #结果 : [ ...
- java8实战:filter的简单使用
<JAVA8实战>中的例子 要实现的功能:通过Apple的color或weight属性,对List<Apple>进行筛选. 1.首先定义com.owl.entity.Apple ...
- nodejs开篇基础<①>
1.安装相关 //安装brew Homebrew ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/insta ...
- linux rabbitmq 远程登录
./rabbitmqctl add_user admin1 admin1 ./rabbitmqctl set_user_tags admin1 administrator ./rabbitmqctl ...