lintcode-427-生成括号
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-生成括号的更多相关文章
- lintcode: 生成括号
生成括号 给定 n 对括号,请写一个函数以将其生成新的括号组合,并返回所有组合结果. 样例 给定 n = 3, 可生成的组合如下: "((()))", "(()())&q ...
- [CareerCup] 9.6 Generate Parentheses 生成括号
9.6 Implement an algorithm to print all valid (e.g., properly opened and closed) combinations of n-p ...
- generate parentheses(生成括号)
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- Leetcode 22.生成括号对数
生成括号对数 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n =3,生成结果为: [ "((()))", "( ...
- [LintCode] Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [LeetCode] Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [leetcode]22. Generate Parentheses生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- Generate parentheses,生成括号对,递归,深度优先搜索。
问题描述:给n对括号,生成所有合理的括号对.比如n=2,(()),()() 算法思路:利用深度优先搜索的递归思想,对n进行深度优先搜索.边界条件是n==0:前面电话号组成字符串也是利用dfs. pub ...
- 022 Generate Parentheses 生成括号
给 n 对括号,写一个函数生成所有合适的括号组合.比如,给定 n = 3,一个结果为:[ "((()))", "(()())", "(())() ...
- [LeetCode] 22. Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
随机推荐
- Windows.Devices API in a C# WinForm Win32 Desktop application in Windows 10
https://social.msdn.microsoft.com/Forums/windows/en-US/40b4f86d-b6d5-430e-a235-7fc003dec4c4/how-to-u ...
- 2018年数学建模国赛B题 智能RGV的动态调度策略
第一种情况大致思路: 每秒判断各个CNC的状态,若工作完成或者是出于空闲状态下则向RGV发出一个请求.同时,RGV每秒判断自己的状态(上下料.移动.闲置.清洗等),如果是处于闲置状态,则启用调度算法, ...
- Go语言 异常panic和恢复recover用法
Go语言 异常panic和恢复recover用法 背景:Go语言追求简洁优雅,所以,Go语言不支持传统的 try…catch…finally 这种异常,因为Go语言的设计者们认为,将异常与控制结构混在 ...
- 【翻译】理解 LSTM 网络
目录 理解 LSTM 网络 递归神经网络 长期依赖性问题 LSTM 网络 LSTM 的核心想法 逐步解析 LSTM 的流程 长短期记忆的变种 结论 鸣谢 本文翻译自 Christopher Olah ...
- [转载]DotNetty 学习
[转载]http://www.cnblogs.com/littlegod/p/7699482.html DotNetty的学习是带着如下这些问题展开: 1. Socket基础框架方案: 通信模式:异步 ...
- 20155226 mini DC 课堂测试补交
由于电脑突然出了点问题,我没有完成mini DC这个测试,现将测试内容及结果补交 题目如下 提交测试截图和码云练习项目链接,实现Linux下dc的功能,计算后缀表达式的值 代码如下 MyDC.clas ...
- 20155306 实验四 Android程序设计
20155306 实验四 Android程序设计 实验内容 1.基于Android Studio开发简单的Android应用并部署测试; 2.了解Android.组件.布局管理器的使用: 3.掌握An ...
- 2017-2018-1 20155315 《信息安全系统设计基础》加分作业:实现mypwd
学习pwd命令 man pwd查看 pwd命令用于显示当前工作目录,是Linux下最常用的命令之一.在不太确定当前位置时,就会使用pwd来判定当前目录在文件系统内的确切位置. 环境变量OLDPWD表示 ...
- Spring之HandlerInterceptor拦截器
思维导图下载:https://pan.baidu.com/s/19z73Bs8MsHFAupga3Cr3Gg
- I/O: std::ios_base::openmode
I/O: std::ios_base::openmode std::ios_base::openmode std::ios_base::in: 打开文件进行读操作,即读取文件中的数据 如果指定路径中 ...