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. STM32F4XX中断方式通过IO模拟I2C总线Master模式

    STM32的I2C硬核为了规避NXP的知识产权,使得I2C用起来经常出问题,因此ST公司推出了CPAL库,CPAL库在中断方式工作下仅支持无子地址 的器件,无法做到中断方式完成读写大部分I2C器件.同 ...

  2. centos7 关闭防火墙

    centos7 关闭防火墙 1.firewall相关的操作    查看防火墙状态 firewall-cmd    --state 关闭防火墙 systemctl  stop   firewalld.s ...

  3. python3使用bencode库实现BT种子生成磁力链接

    python3 需要使用 pip install py3-bencode安装py3-bencode库. pip install py3-bencode 这里使用当前目录下的 1.torrent 文件转 ...

  4. Websocket 临时参考网站

    https://blog.csdn.net/SGuniver_22/article/details/74273839 https://www.zhihu.com/question/20215561 h ...

  5. angular中的$cookies和$cookieStore设置过期时间

    angular1.4及以上版本才支持$cookies. 项目引入的是1.4.2版本,操作cookies原先一直用的是$cookieStore,用的飞起啊. $cookieStore.remove(&q ...

  6. 第13届景驰-埃森哲杯广东工业大学ACM程序设计大赛--J-强迫症的序列

    链接:https://www.nowcoder.com/acm/contest/90/J 来源:牛客网 1.题目描述 牛客网是IT求职神器,提供海量C++.JAVA.前端等职业笔试题库,在线进行百度阿 ...

  7. 20155218 2006-2007-2 《Java程序设计》第一周学习总结

    20155218 2006-2007-2 <Java程序设计>第1周学习总结 教材学习内容总结 浏览教材每章提出一个问题 组建如何与容器互动 PATH与classpath的对比 java的 ...

  8. 无法获得锁 /var/lib/apt/lists/lock - open (11 资源临时不可用)

    具体如下: 1.ps-aux 查出apt-get进程的PID,通常是一个四位数字. 2.用sudo kill PID代码 杀死进程 3.用sudo apt-get update,sudo apt-ge ...

  9. Ruby 配置vimrc

    https://ruby-china.org/topics/19315 mv ~/Downloads/vim-distinguished-develop/colors/*.vim ~/.vim/col ...

  10. WPF 如何自定义一个弹框

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 简述: 手工以原生Grid的方式,自定义了一个仿弹窗效果,优点可以自定义,缺点需要自己实现以及维护整个弹窗的效 ...