题目链接

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:

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


所有组合的个数其实是一个卡特兰数。

我们这样来构造一个合法的字符串:首先第一个位置肯定是“(”,对于后面位置:

1、如果前一个字符是“(”:那么我们可以在当前位置上加入“)”(字符“)”一定有剩余);如果“(”字符还有剩余,也可以在当前位置加入“(”                          本文地址

2、如果前一个字符是“)”:如果当前“(”和“)”剩余的数目不同(即其那面的括号没有完全匹配),可以在当前位置上加入“)”;如果“(”字符还有剩余,也可以在当前位置加入“(”

 1 class Solution {
 2 public:
 3     vector<string> generateParenthesis(int n) {
 4         string tmpres(n<<1, '(');
 5         vector<string> res;
 6         helper(1, tmpres, res, n-1, n);
 7         return res;
 8     }
 9 private:
10     void helper(int index, string &tmpres, vector<string>&res, int leftNum, int rightNum)
11     {
12         if(index >= tmpres.size()){res.push_back(tmpres); return;}
13         if(tmpres[index-1] == '(')
14         {
15             tmpres[index] = ')';
16             helper(index+1, tmpres, res, leftNum, rightNum-1);
17             if(leftNum > 0)
18             {
19                 tmpres[index] = '(';
20                 helper(index+1, tmpres, res, leftNum-1, rightNum);
21             }
22         }
23         else
24         {
25             if(leftNum != rightNum)
26             {
27                 tmpres[index] = ')';
28                 helper(index+1, tmpres, res, leftNum, rightNum-1);
29             }
30             if(leftNum > 0)
31             {
32                 tmpres[index] = '(';
33                 helper(index+1, tmpres, res, leftNum-1, rightNum);
34             }
35         }
36     }
37 };

其实对于某个合法的字符串,我们可以发现从合法字符串的任何一个位置看,“(”的数目 >= ")"的数目,即剩余的“(”的数目 <= 剩余的")"数目, 因此就有以下更加简洁的代码

 1 class Solution {
 2 public:
 3     vector<string> generateParenthesis(int n) {
 4         string tmpres;
 5         vector<string> res;
 6         helper(tmpres, res, n, n);
 7         return res;
 8     }
 9 private:
10     void helper(string &tmpres, vector<string>&res, int leftNum, int rightNum)
11     {
12         if(leftNum > rightNum)return;
13         if(leftNum == 0 && rightNum == 0)
14         {
15             res.push_back(tmpres);
16             return;
17         }
18         if(leftNum > 0)
19         {
20             tmpres.push_back('(');
21             helper(tmpres, res, leftNum-1, rightNum);
22             tmpres.pop_back();
23         }
24         if(rightNum > 0)
25         {
26             tmpres.push_back(')');
27             helper(tmpres, res, leftNum, rightNum-1);
28             tmpres.pop_back();
29         }
30     }
31 };

 

 【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3776583.html

转载于:https://www.cnblogs.com/TenosDoIt/p/3776583.html

LeetCode:Generate Parentheses相关推荐

  1. LeetCode Generate Parentheses

    原题链接在这里:https://leetcode.com/problems/generate-parentheses/ 题目: Given n pairs of parentheses, write ...

  2. [LeetCode] Generate Parentheses

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

  3. [leetcode]Generate Parentheses

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

  4. [leetcode] 22. Generate Parentheses

    题目大意 https://leetcode.com/problems/generate-parentheses/description/ 22. Generate Parentheses Given ...

  5. LeetCode算法入门- Generate Parentheses -day16

    LeetCode算法入门- Generate Parentheses -day16 题目描述 Given n pairs of parentheses, write a function to gen ...

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

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

  7. 【LeetCode每天一题】Generate Parentheses(创造有效的括弧)

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

  8. 【LeetCode从零单排】No22.Generate Parentheses

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

  9. [LeetCode] #22 Generate Parentheses

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

最新文章

  1. Error:Unsupported method: BaseConfig.getApplicationIdSuffix(). The version o
  2. [Python3] 003 变量类型概述 数字类型详叙
  3. 微软开源可扩展存储引擎Extensible Storage Engine
  4. 1.14 日志(递推ybtoj)
  5. Django项目搭建(基础)
  6. 为什么优酷站点限制不了_什么是站点可靠性工程师,为什么要考虑这个职业道路
  7. java 7 的新特性
  8. AI医疗--概念,应用场景及现状解析
  9. OBS无延迟视频直播完整教程(组图)
  10. 关于PC套件显示红外连接出现问题而导致连接不上的解决方案(zz)
  11. 嵌入式学习之QT学习----3 制作简单的QT界面(如:QQ登录界面)
  12. linux深度商店 apt,Deepin系统安装软件总结:通过商店、二进制包、deb包、终端命令安装...
  13. 计算机串口是232吗,RS232串口简介
  14. 33暴力破解(MD5撞击)
  15. 辨大势定内局,解析新时代下的企业新刚需
  16. 格里高利历java_Java 日历笔记
  17. python 老师和父亲_强烈推荐给老师和父母的21部电影
  18. OpenCV | Mat类的copyT、clone、=赋值的区别
  19. 南京工业大学python期末_南京工业大学燃爆期末复习总结
  20. 化学空间对接概念性验证 | Chemical Space Docking

热门文章

  1. node-sass安装报错node-sass@4.12.0 postinstall: `node scripts/build.js`
  2. Java堆溢出错误:java.lang.OutOfMemoryError: Java heap space
  3. linux命令逻辑运算:与、或、非、异或
  4. 通过命令行工具使用阿里云资源编排服务
  5. 《程序是怎样跑起来的》读书笔记——第三章 计算机进行小数运算时出错的原因...
  6. 在创建maven项目时出现: GC overhead limit exceeded 问题
  7. 南阳71(独木舟上的旅行)
  8. Traversing Mapping Filtering Folding Reducing
  9. JS 运行、复制、另存为 代码。
  10. struts2 在线用户记录