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 ...
随机推荐
- c#day03
c#中的随机数 Random random = new Random(); //随机1~200之间的一个数 random.Next(,); //怪兽:防御为10,血量为10 //玩家:随机8~12的攻 ...
- AsyncHelper
http://www.cnblogs.com/zhaopei/p/async_one.html
- Mongodb集群搭建之 Sharding+ Replica Sets集群架构(2)
参考http://blog.51cto.com/kaliarch/2047358 一.概述 1.1 背景 为解决mongodb在replica set每个从节点上面的数据库均是对数据库的全量拷贝,从节 ...
- 备用DNS域名服务器
DNS:1.34.151.129,域名:www#eliuliang#com, 个人用解析地址,请勿使用.
- centos 7怎么通过图形界面来配置静态ip
除了通过修改配置文件的方法来配置静态ip,我们还可以通过图形界面来配置,这样做其实更加方便一点 先进入设置页面 选择网络 我这里是通过有线上网的,我们之间修改配置就可以了 选择ipv4,和manual ...
- windows模拟linux部分功能
--------------------------------------------分割线----------------------------------------------- 系统 wi ...
- sql server与C#中的字符串相等等效写法
sql server两个字段相等判断默认不区分大小写,并且字符串进行Unicode规范化处理. 等效c#中的相等为s=="字符".ToLower().Normalize(Syste ...
- 为DOM节点添加或者删除class
项目中如果应用了常用的javascript类库,多数情况下,这些已经封装好的类库,都会封装一个类似于addClass和removeClass的方法,以便于我们对DOM节点的class进行操作. 以jQ ...
- mysql-5.5.50-winx64
1.help 2.Service 3.Configure 4.User 5.design last 1.获取帮助文档 cd C:\Program Files\mysql\mysql-5.5.50-wi ...
- netfilter/iptables
参考:tcp/ip协议 1.Linux框架概念 1.1.工作流程图 1.2.功能: ①过滤(filter) ②修改源ip.目标ip(nat) ③拆解报文.修改报文标记.重新封装(mangle) ④关闭 ...