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对括号的所有合法排列. 解答: 该问题解的个数就是卡特 ...
随机推荐
- 安装 Homebrew&iterm2&Oh My Zsh
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/instal ...
- 《Clojure编程》笔记 第2章 函数式编程
目录 背景简述 第2章 函数式编程 背景简述 本人是一个自学一年Java的小菜鸡,理论上跟大多数新手的水平差不多,但我入职的新公司是要求转Clojure语言的.坊间传闻:通常情况下,最好是有一定Jav ...
- SpringBoot第四集:整合JdbcTemplate和JPA(2020最新最易懂)
SpringBoot第四集:整合JdbcTemplate和JPA(2020最新最易懂) 当前环境说明: Windows10_64 Maven3.x JDK1.8 MySQL5.6 SpringTool ...
- PHP 教程:Composer 最佳实践
概述 Composer 是 PHP 应用程序的依赖管理器,最初发布于大约 8 年前,2012 年 3 月. 在 php 中使用 Composer 可以提高代码的可重用性,并使你的项目能够轻松地集成来自 ...
- POI做题记录
嘿嘿,偷学一波! 由于博主做的题比较少,所以没按年份整理,直接按照做题时间放上来了. 2020年9月20日 [POI2013]LUK-Triumphal arch 给你一颗\(n\)个点的树(\(n\ ...
- C++实现RTMP协议发送H.264编码及AAC编码的直播软件开发音视频
RTMP(Real Time Messaging Protocol)是专门用来传输音视频数据的流媒体协议,最初由Macromedia 公司创建,后来归Adobe公司所有,是一种私有协议,主要用来联系F ...
- 三分钟快速解析GraphQL基本工作思路!
欢迎阅读 本文会通过实际场景介绍一下 GraphQL,目的是让你快速了解 GraphQL 是什么,以及基本工作思路,不包含实际用法,所以阅读很轻松. 一.GraphQL 是什么? GraphQL 是后 ...
- day88:luffy:支付宝同步结果通知&接收异步支付结果&用户购买记录&我的订单
目录 1.支付宝同步结果通知 2.用户购买记录表 3.接受异步支付结果 4.善后事宜 5.我的订单 1.支付宝同步结果通知 1.get请求支付宝,支付宝返回给你的参数 当用户输入用户名和密码确认支付的 ...
- Spark Standalone模式 高可用部署
本文使用Spark的版本为:spark-2.4.0-bin-hadoop2.7.tgz. spark的集群采用3台机器进行搭建,机器分别是server01,server02,server03. 其 ...
- Spark Shuffle机制详细源码解析
Shuffle过程主要分为Shuffle write和Shuffle read两个阶段,2.0版本之后hash shuffle被删除,只保留sort shuffle,下面结合代码分析: 1.Shuff ...