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 ...
随机推荐
- 1.angular js 学习网址
双向数据绑定: http://html-js.com/article/1863
- QMainFrame类
一.简介: 1.QMainFrame类提供了应用程序的主窗口,因为它可以添加菜单.工具条.状态栏和停靠窗口,同时也支持单文档窗口和多文档窗口,这是它和其他窗口不一样的地方. 2.QMainFrame窗 ...
- 比线程更NB的存在
阅读目录 一 引子 二 协程介绍 三 Greenlet模块 四 Gevent模块 引子 之前我们学习了线程.进程的概念,了解了在操作系统中进程是资源分配的最小单位,线程是CPU调度的最小单位.按道理来 ...
- ACM学习历程—广东工业大学2016校赛决赛-网络赛C wintermelon的魔界寻路之旅(最短路 && 递推)
题目链接:http://gdutcode.sinaapp.com/problem.php?cid=1031&pid=2 题目由于要找对称的路径,那么狠明显可以把右下角的每一块加到左上角对应的每 ...
- bzoj 1257 余数之和 —— 数论分块
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1257 \( \sum\limits_{i=1}^{n}k\%i = \sum\limits_ ...
- Velocity下面的Velocimacros设置
Velocimacros #macro script element允许模板设计者定义一段可重用的VTL template.Velocimacros广泛用于简单和复杂的行列.Velocimacros的 ...
- linux普通用户home目录锁定
- Mybaits整合Spring自动扫描 接口,Mybaits配置文件.xml文件和Dao实体类
1.转自:https://blog.csdn.net/u013802160/article/details/51815077 <?xml version="1.0" enco ...
- Jmeter测试接口简单使用教程
1. 打开 解决 apache-jmeter-2.13 然后进解压后的然后点击bin 文件里面的jmeter.bat 打开jmeter 2. 添加测试组件 1:添 ...
- LoadRunner 参数模拟——快速得到并发用户的进场规则
LoadRunner 并发时,一般需要进入参数化 用户名或者相关的字段数据,用以满足在并发时无限的接近真实的使用场景: 但当你在设置好参数化的读取规则后,在执行并发场景时,用户并发加载顺序是否确实如你 ...