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. Windos10 mysql-8.0.13安装手顺

    一.下载 1.1 官方下载地址:https://dev.mysql.com/downloads/mysql/ ,点击Download 1.2 点击 No thanks,just start my do ...

  2. Binary Indexed Tree (Fenwick Tree)

    Binary Indexed Tree 主要是为了存储数组前缀或或后缀和,以便计算任意一段的和.其优势在于可以常数时间处理更新(如果不需要更新直接用一个数组存储所有前缀/后缀和即可).空间复杂度O(n ...

  3. nodejs fastdfs

    node端fastdfs客户端上传文件 var FdfsClient = require('fdfs'); var fdfs = new FdfsClient({ // tracker servers ...

  4. 几个并发的术语解释——QPS,TPS,PV

    从英文全称翻译出字面意思就OK啦!  PV=page view TPS=transactions per second QPS=queries per second RPS=requests per ...

  5. 20155317王新玮 2006-2007-2 《Java程序设计》第3学习总结

    20155317王新玮 2006-2007-2 <Java程序设计>第3周学习总结 教材学习内容总结 第四章 chothes(String coler,char size)的含义是对col ...

  6. 虚拟机与Linux的初体验

    很早的时候就知道虚拟机这个神奇东西的存在,但也仅仅是只闻其名,未见其身.后来在信息安全素质教育的这门课程上,为了做木马实验.暴力破解实验以及邮件窃取实验,这才比较直接的接触到了虚拟机.当我看着在另一个 ...

  7. [并发并行]_[线程模型]_[Pthread线程使用模型之二 工作组work crew]

    Pthread线程使用模型之二工作组(Work crew) 场景 1.一些耗时的任务,比如分析多个类型的数据, 是独立的任务, 并不像 pipeline那样有序的依赖关系, 这时候pipeline就显 ...

  8. 4040 EZ系列之奖金 (拓扑)

    4040 EZ系列之奖金 时间限制: 1 s 空间限制: 64000 KB 题目等级 : 钻石 Diamond         题目描述 Description 由于无敌的WRN在2015年世界英俊帅 ...

  9. 数据库路由中间件MyCat - 源代码篇(15)

    此文已由作者张镐薪授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. public static void handle(String stmt, ServerConnectio ...

  10. 三、Django安装和流程

    一.MVC模式 MVC(Model-View-Controller),中文名“模型-视图-控制器”,是一个好的Web应用开发所遵循的模式,它有利于把Web应用的代码分解为易于管理的功能模块. M:Mo ...