leetcode-algorithms-22 Generate Parentheses

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. "((()))",
  3. "(()())",
  4. "(())()",
  5. "()(())",
  6. "()()()"
  7. ]

解法

  1. class Solution {
  2. public:
  3. vector<string> generateParenthesis(int n)
  4. {
  5. std::vector<std::string> result;
  6. addParenth(result, "", n, 0);
  7. return result;
  8. }
  9. void addParenth(std::vector<std::string> &v, std::string str, int n, int m)
  10. {
  11. if(n==0 && m==0)
  12. {
  13. v.push_back(str);
  14. return;
  15. }
  16. if(m > 0)
  17. addParenth(v, str+")", n, m-1);
  18. if(n > 0)
  19. addParenth(v, str+"(", n-1, m+1);
  20. }
  21. };

链接: leetcode-algorithms 目录

leetcode-algorithms-22 Generate Parentheses的更多相关文章

  1. [Leetcode][Python]22: Generate Parentheses

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 22: Generate Parentheseshttps://oj.leet ...

  2. 【一天一道LeetCode】#22. Generate Parentheses

    一天一道LeetCode (一)题目 Given n pairs of parentheses, write a function to generate all combinations of we ...

  3. 【LeetCode】22. Generate Parentheses (2 solutions)

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

  4. 【LeetCode】22. Generate Parentheses (I thought I know Python...)

    I thought I know Python... Actually , I know nothing... 这个题真想让人背下来啊,每一句都很帅!!! Given n pairs of paren ...

  5. 【LeetCode】22. Generate Parentheses 括号生成

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:括号, 括号生成,题解,leetcode, 力扣,Pyt ...

  6. LeetCode OJ 22. Generate Parentheses

    题目 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 par ...

  8. LeetCode:22. Generate Parentheses(Medium)

    1. 原题链接 https://leetcode.com/problems/generate-parentheses/description/ 2. 题目要求 给出一个正整数n,请求出由n对合法的圆括 ...

  9. 22. Generate Parentheses(ML)

    22. Generate Parentheses . Generate Parentheses Given n pairs of parentheses, write a function to ge ...

  10. 刷题22. Generate Parentheses

    一.题目说明 这个题目是22. Generate Parentheses,简单来说,输入一个数字n,输出n对匹配的小括号. 简单考虑了一下,n=0,输出"";n=1,输出" ...

随机推荐

  1. 51Nod—1174 区间中最大的数 线段树模版

    在大佬们题解的帮助下算是看懂了线段树吧...在这mark下防一手转头就忘. #include<iostream> #include<stdio.h> using namespa ...

  2. Gym 100247I Meteor Flow(优先队列)

    https://vjudge.net/problem/Gym-100247I 题意:有一艘飞船,现在有n颗流星坠落会攻击到飞船,每颗流星会在t时刻降落,对飞船造成d的伤害,飞船会有一个保护盾,初始值为 ...

  3. 5+ App开发打包指南

    HTML5 Plus应用概述 HTML5 Plus移动App,简称5+App,是一种基于HTML.JS.CSS编写的运行于手机端的App,这种App可以通过扩展的JS API任意调用手机的原生能力,实 ...

  4. python 移动文件夹

    xxx@ddd:~$ mkdir testa testb >>> import shutil >>> shutil.move("/home/xxx/tes ...

  5. phpredis基本操作

    字符串,用于存储变动少的信息 创建对象 $red = Red::create(); 设置值 $red->set('name','张三'); 设置有效期 $red->set('name',' ...

  6. 使用ajax判断登录用户名

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Register.aspx. ...

  7. STL_map.修改删除

    1.修改示例 int TdrSvgAttr::AttrSet_mem(bool _bAttrInStyle, string &_strAttrName, string& _strAtt ...

  8. java基础题集

    1.什么是java虚拟机?为什么java被称作是“平台无关的编程语言”? java虚拟机是一个可以执行java字节码的虚拟机进程.java源文件被编译成能被java虚拟机执行的字节码文件. java被 ...

  9. Codeforces 1062 E - Company

    E - Company 思路: 首先,求出每个点的dfs序 然后求一些点的公共lca, 就是求lca(u, v), 其中u是dfs序最大的点, v是dfs序最小的大点 证明: 假设o是这些点的公共lc ...

  10. putty 显示 ubuntu的文件乱码

    http://www.linuxidc.com/Linux/2012-01/52252.htm 下面几个注意点 1) Windows - Appearance - Font settings 里可以更 ...