#leetcode刷题之路22-括号生成
给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合。
例如,给出 n = 3,生成结果为:
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]
思路:
递归:
#include <iostream>
#include <vector>
using namespace std;
void gen(int left,int right,string s,vector<string> &ans)
{
if(right==) {ans.push_back(s); return ;}//右括号用完前,左括号早就用完了。所以返回
else if(left==right) gen(left-,right,s+'(',ans);//剩余的左括号数量=右括号数量时,只能放左括号
else if(left==) gen(left,right-,s+')',ans);//左括号都写进s中去了,后面只能写右括号了
else {gen(left,right-,s+')',ans);//除上面几种情况外,写左写右均可
gen(left-,right,s+'(',ans);
}
} vector<string> generateParenthesis(int n) {
vector<string> ans;
gen(n,n,"",ans);
return ans;
} int main() {
int n=;
vector<string> ans;
ans=generateParenthesis(n);
cout<<ans[]<<endl;
return ;
}
#leetcode刷题之路22-括号生成的更多相关文章
- LeetCode刷题笔记-回溯法-括号生成
题目描述: 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结果为: [ "((()))", "( ...
- Leetcode之回溯法专题-22. 括号生成(Generate Parentheses)
Leetcode之回溯法专题-22. 括号生成(Generate Parentheses) 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n ...
- #leetcode刷题之路32-最长有效括号
给定一个只包含 '(' 和 ')' 的字符串,找出最长的包含有效括号的子串的长度. 示例 1:输入: "(()"输出: 2解释: 最长有效括号子串为 "()"示 ...
- #leetcode刷题之路20-有效的括号
#include <iostream> #include <string> #include <stack> using namespace std; bool i ...
- python -- leetcode 刷题之路
第一题 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 15], tar ...
- 使用Java+Kotlin双语言的LeetCode刷题之路(三)
BasedLeetCode LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 ...
- 使用Java+Kotlin双语言的LeetCode刷题之路(二)
BasedLeetCode LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 ...
- 使用Java+Kotlin双语言的LeetCode刷题之路(一)
LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 1 两数之和 给定一个整数数 ...
- #leetcode刷题之路40-组合总和 II
给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合.candidates 中的每个数字在每个组合中只能使用一次.说 ...
随机推荐
- linux下C语言三种get输入方式
第一种:scanf() #include "stdio.h" #include "string.h" int main() { ]; scanf("% ...
- Spring MVC基本配置和实现(三)
Item public class Item { private Integer id; private String name; public Integer getId() { return id ...
- 学习笔记:如何阻止Web应用存储敏感数据
在某些情况下,自定义Web应用会保存敏感(专有)数据到用户的缓存文件夹中.如果不重新架构该应用,使用Sysinternals SDelete的注销脚本是否可以确保数据完全被删除且没有任何可恢复残留呢? ...
- .net core系列之《新一代的配置系统Configuration在支持多数据源,热更新,层级化方面代码快速实践》
在我们之前.Net Framework的项目中,配置文件是WebConfig或AppcConfig文件,而当我们想要添加我们自定义的节点时,还需要在这个文件中的section中定义我们自定义的节点,这 ...
- 将NSString变成贝塞尔曲线
将NSString变成贝塞尔曲线 https://github.com/aderussell/string-to-CGPathRef NSString中的字符串是可以通过CoreText框架将其转换成 ...
- mysql在linux下的安装mysql-5.6.33
一.下载源码包 wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz 二.解压源 ...
- Python学习---django之Model语法180124
django之Model语法[Models] 1 django默认支持sqlite,mysql, oracle,postgresql数据库. <1> sqlite django默认使 ...
- [朴孝敏/Loco][Nice Body]
歌词来源:http://music.163.com/#/song?id=28738294 作曲 : 勇敢兄弟/大象王国 [作曲 : 勇敢兄弟/大象王国] 作词 : 勇敢兄弟 [作词 : 勇敢兄弟] A ...
- Java基础知识强化之集合框架笔记76:ConcurrentHashMap之 ConcurrentHashMap简介
1. ConcurrentHashMap简介: ConcurrentHashMap是一个线程安全的Hash Table,它的主要功能是提供了一组和Hashtable功能相同但是线程安全的方法.Conc ...
- python SimpleHTTPServer
Python2 使用的是SimpleHTTPServer python -m SimpleHTTPServer Python3 合并到了http.server python -m http.serve ...