22. Generate Parentheses (recursion algorithm)
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]
AC code:
class Solution {
public:
vector<string> generateParenthesis(int n) {
vector<string> v;
string str = "";
helper(v, str, 0, 0, n);
return v;
}
void helper(vector<string>& v, string str, int left, int right, int n) {
if (right == n) {
v.push_back(str);
return;
}
if (left < n) {
helper(v, str+'(', left+1, right, n);
}
if (right < left) {
helper(v, str+')', left, right+1, n);
}
}
};
Runtime: 0 ms, faster than 100.00% of C++ online submissions for Generate Parentheses.
22. Generate Parentheses (recursion algorithm)的更多相关文章
- [Leetcode][Python]22: Generate Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 22: Generate Parentheseshttps://oj.leet ...
- 22. Generate Parentheses(ML)
22. Generate Parentheses . Generate Parentheses Given n pairs of parentheses, write a function to ge ...
- 刷题22. Generate Parentheses
一.题目说明 这个题目是22. Generate Parentheses,简单来说,输入一个数字n,输出n对匹配的小括号. 简单考虑了一下,n=0,输出"";n=1,输出" ...
- 【LeetCode】22. Generate Parentheses (2 solutions)
Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...
- [LeetCode] 22. 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 ...
- 22. Generate Parentheses——本质:树,DFS求解可能的path
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- Java [leetcode 22]Generate Parentheses
题目描述: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...
- leetcode@ [22]Generate Parentheses (递归 + 卡特兰数)
https://leetcode.com/problems/generate-parentheses/ Given n pairs of parentheses, write a function t ...
随机推荐
- Sqlte 知识点记录
1.表存在 select count(*) from sqlite_master where type='table' and name='MyTable'; sql),path ))"; ...
- Java集合类--->入门下篇
HashSet集合 在上篇大概了解了什么是集合类,知道它可以存储任意类型的对象,并且比数组灵活,集合类的长度可以变化.这里将接着介绍一下,Set接口的实现类之一,HashSet集合,Set集合:元素不 ...
- Linux-NoSQL之MongoDB
1.mongodb介绍 什么是MongoDB ? MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统. 在高负载的情况下,添加更多的节点,可以保证服务器性能. MongoD ...
- QFileInfo与QFileIconProvider(分别用于获取文件信息和获取文件的图标)
判断文件是否存在,获取文件名称,绝对路径,修改时间等等信息 fileInfo = Qt.QFileInfo(filename) fileIcon = Qt.QFileIconProvider() ic ...
- 使用Visual Studio进行单元测试-Part1
写在开头:Coding ain't done until all the tests run. No unit test no BB. -------------------------------- ...
- yum配置文件位置
centos的yum配置文件 cat /etc/yum.conf cachedir=/var/cache/yum //yum 缓存的目录,yum 在此存储下载的rpm 包和数据库,默认设置为/var/ ...
- Azure 用户自定义路由 (User Defined Route)
在公有云环境中,用户创建了一个Vnet,添加了若干个网段后,这几个网段是全联通的状态. 如果希望在Vnet中添加一些功能性的设备,比如防火墙.IPS.负载均衡设备等,就需要进行用户自定义路由的配置. ...
- DCloud-MUI:文档 UI组件
ylbtech-DCloud-MUI:文档 UI组件 1.返回顶部 1.accordion(折叠面板) 折叠面板从二级列表中演化而来,dom结构和二级列表类似,如下: <ul class=&qu ...
- VisualGDB系列7:使用VS创建Linux静态库和动态库
根据VisualGDB官网(https://visualgdb.com)的帮助文档大致翻译而成.主要是作为个人学习记录.有错误的地方,Robin欢迎大家指正. 本文介绍如何在VS中创建静态库和动态库, ...
- IIS及时回收
在打开的列表中更改以下设置:回收——固定时间间隔(分钟) 改为 0进程模型——闲置超时(分钟) 改为 0