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 ...
随机推荐
- 学习tp5的第三天(模型)
一.模型 1.定义基础模型 <?php namespace app\index\model; use think\Model; class User extends Model{ // 设置完整 ...
- doctrine 操作实例(转)
话说这篇文章真是在没有任何实例的情况下帮了大忙 另外附上我自己的一个完整demo:https://github.com/LearnForInterest/material 结合了ci框架的doctri ...
- 浅析Vue.js 中的条件渲染指令
1 应用于单个元素 Vue.js 中的条件渲染指令可以根据表达式的值,来决定在 DOM 中是渲染还是销毁元素或组件. html: <div id="app"> < ...
- bat脚本,winscp,shell加mysql存储过程实现mysql单条数据迁移
起因 公司有个任务,需要迁移mysql中的单条数据.从公司的dev环境到staging环境,dev环境的mysql安装在windows server 2012 R2下,stage是aws的服务器不能直 ...
- 用GO写一个区块链
总结下最近用GO实现区块链实现下面的模块 基本原型 工作量证明,这里用的POW 持久化和命令行,这里用的BoltDB存储区块 地址,这里用的比特币的地址方案 交易 P2P网络,这里为方便本地调试,采用 ...
- freeradius+xl2tp+mysql整合
freeradius+xl2tp+mysql整合 搭了5个小时,可以说是入门到精通了.首先请确认你已经搭建好L2TP,并可以正常使用. 如何在Ubuntu下配置L2TP VPN L2TP使用radi ...
- (数据科学学习手札51)用pymysql来操控MySQL数据库
一.简介 pymysql是Python中专门用来操控MySQL数据库的模块,通过pymysql,可以编写简短的脚本来方便快捷地操控MySQL数据库,本文就将针对pymysql的基本功能进行介绍: 二. ...
- 【洛谷】 3264 [JLOI2015] 管道连接
前言: 如果还不知道斯坦纳树的童鞋可以看这两篇博客: 我的:https://blog.csdn.net/jerry_wang119/article/details/80001711 我一开始学 ...
- python 验证码识别初探
使用 pytesser 与 pytesseract 识别验证码 前置 : 首先需要安装 tesserract tesserract windows 安装包及中文 https://pan.baidu ...
- 【课堂实践】Myod
实验内容 编写MyOD.java 用java MyOD XXX实现Linux下od -tx -tc XXX的功能 实验代码 od.java 截图 遇到的问题及解决办法 一开始想的方向是将得出的功能结果 ...