1. Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
  2.  
  3. For example, given n = 3, a solution set is:
  4.  
  5. "((()))", "(()())", "(())()", "()(())", "()()()"

  

  1. class Solution {
  2. public:
  3. DFS(string s, int left, int right)
  4. {
  5. if(left + right == n * ){
  6. res.push_back(s);
  7. return;
  8. }
  9. if(left == right){
  10. s += '(';
  11. DFS(s, left+, right);
  12. }else if(left > right){
  13. if(left < n){
  14. DFS(s+'(', left+, right);
  15. }
  16. DFS(s+')', left, right +);
  17. }
  18. }
  19. vector<string> generateParenthesis(int n) {
  20. // Start typing your C/C++ solution below
  21. // DO NOT write int main() function
  22. res.clear();
  23. this->n = n;
  24. string s = "";
  25. DFS(s, , );
  26. return res;
  27. }
  28. private:
  29. vector<string> res;
  30. int n;
  31. };

LeetCode_Generate Parentheses的更多相关文章

  1. [LeetCode] Remove Invalid Parentheses 移除非法括号

    Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...

  2. [LeetCode] Different Ways to Add Parentheses 添加括号的不同方式

    Given a string of numbers and operators, return all possible results from computing all the differen ...

  3. [LeetCode] Longest Valid Parentheses 最长有效括号

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

  4. [LeetCode] Generate Parentheses 生成括号

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

  5. [LeetCode] Valid Parentheses 验证括号

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

  6. LeetCode 22. Generate Parentheses

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

  7. 【leetcode】Generate Parentheses

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

  8. No.022:Generate Parentheses

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

  9. 【LeetCode】241. Different Ways to Add Parentheses

    Different Ways to Add Parentheses Given a string of numbers and operators, return all possible resul ...

随机推荐

  1. 设计模式(十五):Iterator迭代器模式 -- 行为型模式

    1.概述 类中的面向对象编程封装应用逻辑.类,就是实例化的对象,每个单独的对象都有一个特定的身份和状态.单独的对象是一种组织代码的有用方法,但通常你会处理一组对象或者集合. 集合不一定是均一的.图形用 ...

  2. 警惕P2B模式

    大家都知道P2P是什么,估计也有很多人了解P2B的意思,这里也不多做解释,但是为什么要警惕P2B,这里我要做详细说明,希望能给大家一个参考.      首先我们要把P2B分成两种,一种是针对大型企业, ...

  3. HDOJ 1196 Lowest Bit(二进制相关的简单题)

    Problem Description Given an positive integer A (1 <= A <= 100), output the lowest bit of A. F ...

  4. ACM2039_三角形三边关系

    #include <iostream> using namespace std; int main(int argc, char* argv[]) { double a,b,c; int ...

  5. iOS之UITableView带滑动操作菜单的Cell

    制作一个可以滑动操作的 Table View Cell 本文翻译自 http://www.raywenderlich.com/62435/make-swipeable-table-view-cell- ...

  6. 学习手机游戏开发的两个方向 Cocos2d-x 和 Unity 3D/2D,哪个前景更好?

    如题! 首先说一说学习手机游戏(移动游戏)这件事. 眼下移动互联网行业的在以井喷状态发展.全球几十亿人都持有智能终端设备(ios android),造就了非常多移动互联网创业机会: 一.移动社交 微信 ...

  7. Linux常见面试题

      一.填空题:1. 在Linux系统中,以 文件 方式访问设备 .2. Linux内核引导时,从文件 /etc/fstab 中读取要加载的文件系统.3. Linux文件系统中每个文件用 索引节点来标 ...

  8. 忘记 mysql5.5.24 数据库 root 密码

    兹整理如下,供网友参考 第一步:关闭mysql服务 第二步:新建txt 写入如下内容 UPDATE mysql.user SET Password=PASSWORD('blog.const.net.c ...

  9. linux一键安装vncserver脚本

    title: linux一键安装vncserver脚本 date: 2016-04-11 14:32:04 tags: --- linux多数情况下是作为服务器使用的,管理员一般也喜欢使用命令行来管理 ...

  10. 打开新窗口(window.open)

    open() 方法可以查找一个已经存在或者新建的浏览器窗口. 语法: window.open([URL], [窗口名称], [参数字符串]) 参数说明: URL:可选参数,在窗口中要显示网页的网址或路 ...