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 ...
随机推荐
- Js基础知识3-字符串、正则表达式全解
字符串的生成转换 你可以将任何类型的数据都转换为字符串,你可以用下面三种方法的任何一种: var myStr = num.toString(); // "19" var myStr ...
- python之路----面向对象进阶二
item系列 __getitem__\__setitem__\__delitem__ class Foo: def __init__(self,name,age,sex): self.name = n ...
- win7 安装.Net framework 4.0出现 安装不成功,错误代码0x80240037 的解决方法
1.安装说明 系统:win7 64位 安装包:dotNetFx40_Full_x86_x64.exe(.Net framework 4.0) 出现的问题:在win7 上安装dotNetFx40_Ful ...
- Python3 打开 https 链接,异常:“SSL: CERTIFICATE_VERIFY_FAILED”
Python3 打开 https 链接,异常:“SSL: CERTIFICATE_VERIFY_FAILED” 一.问题 Python2.7.9 之后,当使用urllib.urlopen打开一个 ht ...
- 07:urllib与urllib2基本使用
参考博客:https://blog.csdn.net/chendong_/article/details/51973499 1.1 urllib2发送get请求 # -*- coding:UTF-8 ...
- Fast特征点的寻找和提取
一.基础 最初由Rosten和Drummond [Rosten06]提出的FAST(加速段测试的特征)特征检测算法是基于将点P与其包围圆内的点集的直接比较的思想. 基本思想是,如果附近的几个点与P类似 ...
- Duilib 实现开关按钮
转载:http://blog.csdn.net/wuan584974722/article/details/25045737 我们在做MFC程序时候经常会一个切换式的按钮,之前我的做法是利用butti ...
- 51nod 1009 数字1的数量
1009 数字1的数量 给定一个十进制正整数N,写下从1开始,到N的所有正数,计算出其中出现所有1的个数. 例如:n = 12,包含了5个1.1,10,12共包含3个1,11包含2个1,总共5 ...
- 51nod 1106 质数检测
#include <bits/stdc++.h> using namespace std; int n; ; bool s[maxn]; void is_prime() { memset( ...
- mysql 对比两个表的一致性
-- A和B两个表 数据是否一致select 自定义 from A left join B on A.id = B.id where B.id is null 注释:这样查询的结果是A表中有而B表中没 ...