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 ...
随机推荐
- Maximum execution time of 30 seconds exceeded解决错误方法
Maximum execution time of 30 seconds exceeded解决错误方法Fatal error: Maximum execution time of 30 seconds ...
- 检测u盘是否挂载上方法
打开内核log:echo "8" > /proc/sys/kernel/printk 关闭内核log:echo "1" > /proc/sys/ke ...
- SDOI2019Round1游记
SDOI2019Round1游记 Day 0 报道日,早晨五点睡的觉,一觉醒来已经一点半了,收拾收拾东西报道去了.因为没吃饭,坐着出租车晕车了,我让师傅把我放到历下大润发,我去金拱门吃了点饭才去的23 ...
- P2P原理及UDP穿透简单说明
转:http://http://andylin02.iteye.com/blog/444666 P2P原理及UDP穿透简单说明 本文章出自cnntec.com的AZ猫著,如需要转发,请注明来自cnnt ...
- adb shell 命令详解,android, adb logcat
http://www.miui.com/article-275-1.html http://noobjava.iteye.com/blog/1914348 adb shell 命令详解,android ...
- JavaScript Match
JavaScript Match 版权声明:未经授权,严禁转载! 随机数 // 随机数 Math.random() 随机生成一个大于等于0且小于1的小数. // 0>= r < 1 [0, ...
- 02: vue.js常用指令
目录:Vue其他篇 01: vue.js安装 02: vue.js常用指令 目录: 1.1 vuejs简介 1.2 选择器:根据id.class等查找 1.3 静态绑定数据 data 1.4 插值 { ...
- 09: TemplateView , ListView ,DetailView三种常用类视图用法
1.1 视图混合介绍 1.Mixin和View的职能区分 1. Mixin提供数据,View提供模板和渲染,所以一般get_context_data在Mixin中,get(),post(),head( ...
- 立几个flag
有时候会心血来潮想学一点东西,然后搞别的东西的时候就慢慢忘了.. 这里做个备忘录: 树分块/树上莫队 广义后缀自动机(大概这辈子都不会去学了) 带花树(如果我能学的动那个线代的随机算法就放弃这个) 模 ...
- scp命令在linux间传送文件的方法
当两台LINUX主机之间要互传文件时可使用SCP命令来实现,建立信任关系之后可不输入密码. 把你的本地主机用户的ssh公匙文件复制到远程主机用户的~/.ssh/authorized_keys文件中 ...