LeetCode 22. Generate Parentheses
2024-08-27 05:26:42
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:
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]
public class Solution {
private List<String> list = new ArrayList<String>(); public List<String> generateParenthesis(int n) {
run("", n, 0);
return list;
} // l 为剩余左括号数,r为剩余未匹配的右括号数目
public void run(String str, int l, int r) {
if (l == 0 && r == 0) {
list.add(str);
return; //跳出run方法
}
if (l > 0) {
String s = str + "(";
run(s, l-1, r+1);
}
if (r > 0) {
String s = str + ")";
run(s, l, r-1);
}
}
}
程序结果:
最新文章
- 24. Swap Nodes in Pairs
- JAVA CDI 学习(1) - @Inject基本用法
- 从 Eclipse 迁移至 Android Studio
- How to Shorten the Paper
- 做mapx、ArcEngine的二次开发出现“没有注册类别 (异常来自 HRESULT:0x80040154 (REGDB_E_CLASSNOTREG)”
- HTML-day-2-HTML常用标签
- Pitcher Rotation
- 关于OJ的输入和输出(转)
- System.Windows.Forms中的Message Structure
- AngularJs学习笔记7——四大特性之模块化设计
- python 获取当前时间
- 带金属光泽的模型shader的实现
- jQuery之jquery.lazyload.js插件用法
- Flask 单元测试 unittest
- swiper轮播在ie浏览器上遇到的显示问题探索
- 后端判断用户是否关闭浏览器(关闭网站相关的全部tab)
- 什么是ScaleIO中的forwards rebuild和backwards rebuild?
- (转)SSIS_数据流转换(Union All&;合并联接&;合并)
- springboot自定义配置文件
- HDOJ.1113 Word Amalgamation(map)
热门文章
- BZOJ4516: [Sdoi2016]生成魔咒
- Express4+Mongodb超简单入门实例
- sql 批量插入数据到Sqlserver中 效率较高的方法
- 使用ab对nginx进行压力测试
- Java IO流学习总结
- 苹果iphone手机上input的button按钮颜色显示有问题,安卓却没问题
- hibernate学习三(使用Annotation,注解)
- .htaccess语法之RewriteCond与RewriteRule指令格式详细解释
- mysql5.x升级到mysql5.7后导入之前数据库date出错的快速解决方法【mysql低版本数据导入到高版本出错的解决方法】
- NoSQL指南