题目大意:给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(构造)的更多相关文章

  1. [LeetCode] 22. Generate Parentheses 生成括号

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  2. leetcode@ [22]Generate Parentheses (递归 + 卡特兰数)

    https://leetcode.com/problems/generate-parentheses/ Given n pairs of parentheses, write a function t ...

  3. LeetCode 22. Generate Parentheses

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  4. Java [leetcode 22]Generate Parentheses

    题目描述: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...

  5. [leetcode]22. Generate Parentheses生成括号

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  6. 蜗牛慢慢爬 LeetCode 22. Generate Parentheses [Difficulty: Medium]

    题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...

  7. [LeetCode] 22. Generate Parentheses ☆☆

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  8. [LeetCode]22. Generate Parentheses括号生成

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  9. LeetCode 22 Generate Parentheses(找到所有匹配的括号组合)

    题目链接 : https://leetcode.com/problems/generate-parentheses/?tab=Description   给一个整数n,找到所有合法的 () pairs ...

随机推荐

  1. c/c++日期时间处理与字符串string转换

    转自:https://www.cnblogs.com/renjiashuo/p/6913668.html 在c/c++实际问题的编程中,我们经常会用到日期与时间的格式,在算法运行中,通常将时间转化为i ...

  2. web前端----Bootstrap框架

    Bootstrap介绍 Bootstrap是Twitter开源的基于HTML.CSS.JavaScript的前端框架. 它是为实现快速开发Web应用程序而设计的一套前端工具包. 它支持响应式布局,并且 ...

  3. Centos下安装git高版本2.1.2

    安装依赖软件 # yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel asciidoc # yum in ...

  4. centos 安装最新稳定版本docker

    直接yum安装的docker版本是 : docker --versionDocker version 1.12.6, build 85d7426/1.12.6 一些新特性需要安装最新的稳定版本 国内可 ...

  5. 20145127 《Java程序设计》第五次实验报告

    实验简述: 在本周,我们进行了Java的第五次试验,本次实验的主要内容是结对编程.本次实验的大体过程是: 1.先进行Java的客户端与服务端的代码编写.结对是两个人,一人负责客户端,一人负责服务端. ...

  6. 2018-2019-1 20189218《Linux内核原理与分析》第七周作业

    task_struck数据结构 在Linux内核中,通过task_struct这个结构体对进程进行管理,我们可以叫他PCB或者进程描述符.这个结构体定义在include/linux/sched.h中. ...

  7. TableView,自定义TableViewCell

    自定义Table 原理: http://blog.jobbole.com/67272/ http://www.cnblogs.com/wangxiaofeinin/p/3532831.html 补充: ...

  8. Python3基础 if elif 示例 判断一个数在哪个区间内

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  9. Python3基础 str casefold 返回全是小写字母的新字符串

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  10. linux下的下载之道

    a.场景: 平时会使用百度网盘下载电影,但是用ios版的百度云实在是有点慢,而平时不用windows,因此只能在linux上寻找解决之道. b.背景(我正在使用中): linux:ubuntu 14. ...