题意:

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

For example, given n = 3, a solution set is: (Medium)

[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]

分析:

生成满足数量要求的所以可能的匹配的括号。实际就是一道回溯或者深搜题。

当左括号还没有达到数量的时候,可以添加左括号;当右括号比左括号数量少的时候可以添加右括号;

搜索所有情况即可。有了昨天的17题练习过之后,这个回溯写的就比较顺手了。

代码:

 class Solution {
void dfs(vector<string>& v, string& s, int n,int left) {
if (s.size() == n) {
v.push_back(s);
return;
}
int right = s.size() - left;
if (left < n / ) {
s.push_back('(');
dfs(v,s,n,left+);
s.pop_back();
}
if (right < left) {
s.push_back(')');
dfs(v,s,n,left);
s.pop_back();
}
return;
}
public:
vector<string> generateParenthesis(int n) {
vector<string> result;
if (n == ) {
return result;
}
string s;
dfs(result,s,*n,);
return result;
}
};

LeetCode22 Generate Parentheses的更多相关文章

  1. Leetcode22. Generate Parentheses(生成有效的括号组合)

    (尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/74937307冷血之心的博客) 题目如下:

  2. Leetcode22.Generate Parentheses括号生成

    给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结果为: [ "((()))", "(()())& ...

  3. LeetCode 22. 括号生成(Generate Parentheses)

    22. 括号生成 22. Generate Parentheses 题目描述 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结 ...

  4. 72. Generate Parentheses && Valid Parentheses

    Generate Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...

  5. Generate Parentheses

    Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...

  6. [LintCode] Generate Parentheses 生成括号

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

  7. [CareerCup] 9.6 Generate Parentheses 生成括号

    9.6 Implement an algorithm to print all valid (e.g., properly opened and closed) combinations of n-p ...

  8. 【题解】【排列组合】【回溯】【Leetcode】Generate Parentheses

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

  9. leetcode022. Generate Parentheses

    leetcode 022. Generate Parentheses Concise recursive C++ solution class Solution { public: vector< ...

随机推荐

  1. Codeforces Educational Codeforces Round 15 D. Road to Post Office

    D. Road to Post Office time limit per test 1 second memory limit per test 256 megabytes input standa ...

  2. Speex for Android

    http://blog.csdn.net/chenfeng0104/article/details/7088138在Android开发中,需要录音并发送到对方设备上.这时问题来了,手机常会是GPRS. ...

  3. UVALive 5888 Stack Machine Executor (栈+模拟)

    Stack Machine Executor 题目链接: http://acm.hust.edu.cn/vjudge/problem/26636 Description http://7xjob4.c ...

  4. python's descriptor II

    [python's descriptor II] For instance, a.x has a lookup chain starting with a.__dict__['x'], then ty ...

  5. JavaScript 核心参考教程 内置对象

    这个标准基于 JavaScript (Netscape) 和 JScript (Microsoft).Netscape (Navigator 2.0) 的 Brendan Eich 发明了这门语言,从 ...

  6. oracle学习 五 使用存储过程创建一个重置密码为123456的功能(持续更新中)

    首先写一个函数是MD5的方法 create FUNCTION GET_MD5 ( p_str in varchar2) RETURN varchar2 IS BEGIN RETURN Utl_Raw. ...

  7. 埃氏筛法(快速筛选n以内素数的个数)

    给你一个数n,请问n以内有多少个素数?(n <= 10e7) 一般来说,要是对一个整数进行素数判断,首先想到的是写个函数判断是否为素数,然后调用这个函数,时间复杂度为O(n^(½)),但是要求n ...

  8. JDBC Connection

    [ http://shift-alt-ctrl.iteye.com/blog/1967020]   关于JDBC中关于Connection的两个疑问:   1.Connection实例是线程安全的吗? ...

  9. centos下 Apache、php、mysql默认安装路径

    centos下 Apache.php.mysql默认安装路径 http://blog.sina.com.cn/s/blog_4b8481f70100ujtp.html apache: 如果采用RPM包 ...

  10. XGrid绑定(转)

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Windows.Fo ...