题目:

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

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

"((()))", "(()())", "(())()", "()(())", "()()()"

代码:

class Solution {
public:
vector<string> generateParenthesis(int n)
{
vector<string> ret;
vector<char> tmp;
Solution::dfs(ret, tmp, , n, n, n);
return ret;
}
static void dfs( vector<string>& ret, vector<char>& tmp, int sum, int left, int right, int n )
{
if ( left== && right== )
{
ret.push_back(string(tmp.begin(),tmp.end()));
return;
}
// '(' or ')'
if ( left> )
{
tmp.push_back('(');
Solution::dfs(ret, tmp, sum+, left-, right, n);
tmp.pop_back();
}
if ( right> && sum> )
{
tmp.push_back(')');
Solution::dfs(ret, tmp, sum-, left, right-, n);
tmp.pop_back();
}
} };

tips:

第一反应是不是stack相关的解法,因为感觉跟逆波兰表达式差不多。

后来发现用‘深搜+剪枝’即可解决。

这里left表示没有使用的'(',right表示没有使用的')';sum标记使用left和right的情况,使用一次left给sum加1,使用一次right给sum减1。

能迭代下去的核心条件是:使用的sum必须大于等于0.

1. 深搜情况,这里每层有两个分支:'(' 或者 ')'

2. 剪枝条件:

  a) 只要使用过'('小于n, 则可以加到tmp中

  b) 使用过的')'小于n, 并且当前的sum是大于0的

=======================================

第二次用dfs过这道题,套路稍微熟悉一些了,考虑dfs的分支顺序:每一层可以加入left括号也可以加入right括号,终止条件的是剩余的left括号数量一定小于right括号的数量。

【Generate Parentheses】cpp的更多相关文章

  1. 【Valid Parentheses】cpp

    题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...

  2. 【Longest Valid Parentheses】cpp

    题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...

  3. hdu 4739【位运算】.cpp

    题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...

  4. Hdu 4734 【数位DP】.cpp

    题意: 我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~ ...

  5. 【Valid Sudoku】cpp

    题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  6. 【Permutations II】cpp

    题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...

  7. 【Subsets II】cpp

    题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...

  8. 【Sort Colors】cpp

    题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...

  9. 【Sort List】cpp

    题目: Sort a linked list in O(n log n) time using constant space complexity. 代码: /** * Definition for ...

随机推荐

  1. grunt + sass 使用记录

    环境依赖 Nodejs for grunt Ruby for sass 配置文件 package.json { "name": "app", "ver ...

  2. 【作业留存】根据IATF框架,设计的一种中小型企业安全拓扑

  3. Java 中 Double 相关问题

    在项目当中,对于double类型数据的使用比较频繁.尤其是处理金钱相关的数据,在使用Double类型的数据时,涉及到精度,显示,四舍五入等等问题. 1.  显示问题,当double 数据 小于 0.0 ...

  4. 利用binlog2sql解析mysqlbinlog row行日志

    binlog2sql 项目作者:曹单锋 github项目地址:https://github.com/danfengcao/binlog2sql 也可在github.com上搜索“binlog2sql” ...

  5. log4j.properties中的这句话“log4j.logger.org.hibernate.SQL=DEBUG ”该怎么写在log4j.xml里面呢?

    http://www.cnblogs.com/gredswsh/p/log4j_xml_properties.html 请问:log4j.properties中的这句话“log4j.logger.or ...

  6. IOS 强指针(strong)和弱指针(weak)

    // strong 强指针        // weak 弱指针        // ARC, 只要对象没有强指针就会自动释放        // OC中默认都是强指针

  7. 实现带复选框的TreeView控件

    实现效果: 知识运用: TreeView控件的CheckView属性 //是否在树形视图控件中显示复选框 public bool CheckBoxs{ get;ser } 实现代码: TreeView ...

  8. UVA1629 Cake slicing

    题目传送门 直接暴力定义f[x1][y1][x2][y2]是使对角为\((x1, y1),(x2, y2)\)这个子矩形满足要求的最短切割线长度 因为转移顺序不好递推,采用记忆化搜索 #include ...

  9. css中如何把鼠标变成手

    css中鼠标放上去变成手型怎么设置:其实就是一个属性的问题, css的cursor属性 cursor:pointer; 其实这个属性我也记了很多,到现在都容易拼写错误,不过好在编辑器有提示. defa ...

  10. GVIM——简直美如画,有没有!

    "========================================== " Author: wklken " Version: 9.1 " Em ...