LeetCode 022 Generate Parentheses
题目描述:Generate Parentheses
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:
"((()))", "(()())", "(())()", "()(())", "()()()"
分析:
递归
代码如下:
class Solution {
private:
vector<string> ret;
public:
void solve(int dep, int maxDep, int leftNum, int leftNumTotal, string s)
{
//如果(个数超出,return
if (leftNumTotal * 2 > maxDep)
return;
//如果长度足够,return
if (dep == maxDep){
ret.push_back(s);
return;
}
for(int i = 0; i < 2; i++)
//加 (
if (i == 0)
solve(dep + 1, maxDep, leftNum + 1, leftNumTotal + 1, s + '(');
//加 )
else if (leftNum > 0)
solve(dep + 1, maxDep, leftNum - 1, leftNumTotal, s + ')');
}
vector<string> generateParenthesis(int n){
ret.clear();
solve(0, 2 * n, 0, 0, "");
return ret;
}
};
Java:
public List<String> generateParenthesis(int n) {
ArrayList<String> result = new ArrayList<String>();
dfs(result, "", n, n);
return result;
}
public void dfs(ArrayList<String> result, String s, int left, int right) {
if (left > right) {
return;
}
if (left == 0 && right == 0) {
result.add(s);
return;
}
if (left > 0) {
dfs(result, s + "(", left - 1, right);
}
if (right > 0) {
dfs(result, s + ")", left, right - 1);
}
}
LeetCode 022 Generate Parentheses的更多相关文章
- 【JAVA、C++】LeetCode 022 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 (递归 + 卡特兰数)
https://leetcode.com/problems/generate-parentheses/ Given n pairs of parentheses, write a function t ...
- [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 ...
- 【leetcode】Generate Parentheses
题目简述: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...
- 【leetcode】 Generate Parentheses (middle)☆
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之 Generate Parentheses
题目:http://oj.leetcode.com/problems/generate-parentheses/ 描述:给定一个非负整数n,生成n对括号的所有合法排列. 解答: 该问题解的个数就是卡特 ...
随机推荐
- CF1320C World of Darkraft: Battle for Azathoth
线段树 又是熟悉的感觉,又是E题写完了,没调完,不过还好上了紫 CF1295E 可以发现可以打败怪兽的关系类似二维偏序 那么首先考虑第一维(武器)以攻击值($a_{i}$)进行排序 把所有的怪兽以防御 ...
- NOIP 2013 P1967 货车运输
倍增求LCA+最大生成树 题目给出的是一张图,在图上有很多算法无法实现,所以要将其转化为树 题中可以发现货车的最后的载重量是由权值最小的一条边决定的,所以我们求最大生成树 求完最大生成树后我们得到一个 ...
- 天啦撸!打印日志竟然只晓得 Log4j?
空了的时候,我都会在群里偷偷摸摸地潜水,对小伙伴们的一举一动.一言一行筛查诊断.一副班主任的即时感,让我感到非常的快乐,略微夹带一丝丝的枯燥. 这不,我在战国时代读者群里发现了这么一串聊天记录: 竟然 ...
- EMP-面向未来微前端方案正式开源了!
原文团队掘金平台:https://juejin.im/post/6891532248269783054 EMP项目github链接: https://github.com/efoxTeam/emp E ...
- Spider_基础总结5--动态网页抓取--元素审查--json--字典
# 静态网页在浏览器中展示的内容都在HTML的源码中,但主流网页使用 Javascript时,很多内容不出现在HTML的源代码中,此时仍然使用 # requests+beautifulsoup是不能够 ...
- tcp 保活定时器分析 & Fin_WAIT_2 定时器
tcp keepalive定时器 http server 和client端需要防止"僵死"链接过多!也就是建立了tcp链接,但是没有报文交互, 或者client 由于主机突然掉电! ...
- tcpack---1简述
TCP重传机制 TCP要保证所有的数据包都可以到达,所以,必需要有重传机制. 超时重传机制 一种是不回ack,死等3,当发送方发现收不到3的ack超时后,会重传3.一旦接收方收到3后,会ack 回 4 ...
- spring中的事务有两种方式
1种是我们常用的声明式事务,如注解,或者配置文件配置的. 2种是编程式事务,如 TransactionTemplate 类的使用.
- ceph各个版本之间参数变化分析
前言 本篇主要是分析ceph的版本之间参数的变化,参数变化意味着功能的变化,通过参数来分析增加,删除,修改了哪些功能,以及版本之间的变化,本篇主要通过导出参数,然后通过脚本去比对不同的版本的参数变化 ...
- RedisEclipse
1.Eclipse配置 2.HelloWorld import redis.clients.jedis.Jedis; public class TestPing { public static voi ...