LeetCode 22. Generate Parentheses(构造)
题目大意:给n个'(' 和 ')',构造出所有的长度为2*n并且有效的(可匹配的)字符串。
题目分析:这道题不难,可以直接搜索出所有可能的字符串,然后再逐一判断是否合法即可。但是还有更好的办法,实际上,“判断是否合法”这一操作是冗余的,如果可以直接朝着满足可匹配性的方向进行构造,就可避免这一冗余操作,这需要换一个角度思考。
一个有效(可匹配)字符串中, '(' 的个数决定了 ')' 的个数(从左向右看)。所以,初始时,可以看成是 “有n个 '(' 还没有用、有0个 ')' 必须要用”。这样,在搜索的时候就能避开无效(不可匹配)的情况。
代码如下:
class Solution {
private:
void dfs(vector<string>& s,string p,int n,int m){///已经构造好的字符串为p,还剩n个'('可用,并且必须要用m个')'。
if(n==0&&m==0){
s.push_back(p);
return ;
}
if(m>0) dfs(s,p+')',n,m-1);
if(n>0) dfs(s,p+'(',n-1,m+1);
}
public:
vector<string> generateParenthesis(int n) {
vector<string>s;
dfs(s,"",n,0);
return s;
}
};
LeetCode 22. Generate Parentheses(构造)的更多相关文章
- [LeetCode] 22. Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- leetcode@ [22]Generate Parentheses (递归 + 卡特兰数)
https://leetcode.com/problems/generate-parentheses/ Given n pairs of parentheses, write a function t ...
- LeetCode 22. Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- Java [leetcode 22]Generate Parentheses
题目描述: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...
- [leetcode]22. Generate Parentheses生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 蜗牛慢慢爬 LeetCode 22. Generate Parentheses [Difficulty: Medium]
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- [LeetCode] 22. Generate Parentheses ☆☆
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [LeetCode]22. Generate Parentheses括号生成
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- LeetCode 22 Generate Parentheses(找到所有匹配的括号组合)
题目链接 : https://leetcode.com/problems/generate-parentheses/?tab=Description 给一个整数n,找到所有合法的 () pairs ...
随机推荐
- Mysql的基本语句
Mysql的基本语句 1.查询当前数据库所有表名: -- 方案一: show tables; --方案二:jeesite为数据库 select table_name from information_ ...
- 20145332卢鑫 WEB安全基础实验
20145332 WEB安全基础实验 实验过程 ·SQL字符串注入 ·Database Backdoors step1 ·Database Backdoors step2 ·Phishing with ...
- sql逻辑查询语句的执行顺序
SELECT语句关键字的定义顺序 SELECT DISTINCT <select_list> FROM <left_table> <join_type> JOIN ...
- JavaScript:Function/Object/prototype/__proto__
console.log(Object.__proto__===Function.prototype); //true console.log(Object.prototype.__proto__); ...
- HDU 1848 Fibonacci again and again(SG函数入门)题解
思路:SG打表 参考:SG函数和SG定理[详解] 代码: #include<queue> #include<cstring> #include<set> #incl ...
- User-Defined Table Types 用户自定义表类型
Location 数据库--可编程性--类型--用户定义表类型 select one database--> programmability-->types-->user--defi ...
- 1、Python快速入门(0529)
学习来自马哥教育的视频,感谢马哥 编程语言: 用户: 问题空间 计算机:解决问题 解空间 抽象: 机器代码-->微码编程-->高级语言 (语言的高下级的是根据语言是否被人类容易理解或者更接 ...
- MySQL查询优化之性能提升一个数量级
这段时间一直在用kettle做数据抽取和报表,写SQL便是家常便饭了,200行+SQL经常要写.甚至写过最长的一个SQL500多行将近600行.这么长的SQL估计大部分人连看的意愿都没有,读起来也比较 ...
- MVC ---- EF的安装于卸载
先卸载EF:Uninstall-Package EntityFramework -Force 在安装EF5.0:Install-Package EntityFramework –Version 5.0 ...
- LINUX设备驱动模型之class
转自 https://blog.csdn.net/qq_20678703/article/details/52754661 1.LINUX设备驱动模型中的bus.device.driver,.其中bu ...