427-生成括号

给定 n 对括号,请写一个函数以将其生成新的括号组合,并返回所有组合结果。

样例

给定 n = 3, 可生成的组合如下:

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

标签

回溯法 递归 字符串处理 谷歌 Zenefits

思路

使用回溯+递归

code

class Solution {
public:
/**
* @param n n pairs
* @return All combinations of well-formed parentheses
*/
vector<string> generateParenthesis(int n) {
// Write your code here
if (n <= 0) {
return vector<string>();
}
vector<string> result;
string temp;
generateParenthesis(n, temp, result, 0, 0);
return result;
} void generateParenthesis(int n, string temp, vector<string> &result, int lCount, int rCount) {
if (lCount == n) {
for (int i = rCount; i < n; i++) {
temp += ')';
}
result.push_back(temp);
return;
}
generateParenthesis(n, temp + '(', result, lCount + 1, rCount);
if (lCount > rCount) {
generateParenthesis(n, temp + ')', result, lCount, rCount + 1);
}
}
};

lintcode-427-生成括号的更多相关文章

  1. lintcode: 生成括号

    生成括号 给定 n 对括号,请写一个函数以将其生成新的括号组合,并返回所有组合结果. 样例 给定 n = 3, 可生成的组合如下: "((()))", "(()())&q ...

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

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

  3. generate parentheses(生成括号)

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

  4. Leetcode 22.生成括号对数

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

  5. [LintCode] Generate Parentheses 生成括号

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

  6. [LeetCode] Generate Parentheses 生成括号

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

  7. [leetcode]22. Generate Parentheses生成括号

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

  8. Generate parentheses,生成括号对,递归,深度优先搜索。

    问题描述:给n对括号,生成所有合理的括号对.比如n=2,(()),()() 算法思路:利用深度优先搜索的递归思想,对n进行深度优先搜索.边界条件是n==0:前面电话号组成字符串也是利用dfs. pub ...

  9. 022 Generate Parentheses 生成括号

    给 n 对括号,写一个函数生成所有合适的括号组合.比如,给定 n = 3,一个结果为:[  "((()))",  "(()())",  "(())() ...

  10. [LeetCode] 22. Generate Parentheses 生成括号

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

随机推荐

  1. php文件相关操作

    //遍历目录及文件 function myBianli($dirname){ //1.打开 $dir = opendir($dirname); //2.读取 while($filename = rea ...

  2. aiohttp的模板

    import aiohttp import asyncio import async_timeout from urllib.parse import urljoin,urldefrag root_u ...

  3. wav2midi 音乐旋律提取算法 附可执行demo

    前面提及过,音频指纹算法的思路. 也梳理开源了两个比较经典的算法. https://github.com/cpuimage/shazam https://github.com/cpuimage/Aud ...

  4. node从搭建运行项目整体流程

    1. 初始化配置基本信息: npm init (自定义配置) npm init -y (一切配置采用默认值) 在当前目录产生package.json文件,有一个dependencies用来记录该项目所 ...

  5. 推荐一个学习Flex chart的好网站

    推荐一个学习Flex chart的好网站 2013-03-04 14:16:56|  分类: Flex |  标签: |字号大中小 订阅     推荐一个学习Flex chart的好网站 最近在做一个 ...

  6. 20155236 2016-2017-2《Java程序设计》课程总结

    20155236 2016-2017-2<Java程序设计>课程总结 作业链接汇总 预备作业1:对专业学习的展望,课程方面.师生关系的建议 预备作业2:在实践中学习,自己的相关经验以及C语 ...

  7. String类使用

    String类的使用 String类 String类在java.lang包中,java使用String类创建一个字符串变量,字符串变量属于对象.java把String类声明的final类,不能有类.S ...

  8. 2017-2018-1 20155318 《信息安全系统设计基础》第2周课堂实践、makefile、以及myod

    2017-2018-1 20155318 <信息安全系统设计基础>第2周课堂实践.makefile.以及myod 测试3-gdb测试 用gcc -g编译vi输入的代码 在main函数中设置 ...

  9. 20155331 2016-2017-2 《Java程序设计》

    20155331 2016-2017-2 <Java程序设计> 教材学习内容总结 理解封装,继承和多态. 封装最简单的理解就是包装,把编译的class文件封装起来,便于管理,还可以设置密码 ...

  10. Discover Feature Engineering, How to Engineer Features and How to Get Good at It

    Feature engineering is an informal topic, but one that is absolutely known and agreed to be key to s ...