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);
}
}
}

程序结果:

最新文章

  1. 24. Swap Nodes in Pairs
  2. JAVA CDI 学习(1) - @Inject基本用法
  3. 从 Eclipse 迁移至 Android Studio
  4. How to Shorten the Paper
  5. 做mapx、ArcEngine的二次开发出现“没有注册类别 (异常来自 HRESULT:0x80040154 (REGDB_E_CLASSNOTREG)”
  6. HTML-day-2-HTML常用标签
  7. Pitcher Rotation
  8. 关于OJ的输入和输出(转)
  9. System.Windows.Forms中的Message Structure
  10. AngularJs学习笔记7——四大特性之模块化设计
  11. python 获取当前时间
  12. 带金属光泽的模型shader的实现
  13. jQuery之jquery.lazyload.js插件用法
  14. Flask 单元测试 unittest
  15. swiper轮播在ie浏览器上遇到的显示问题探索
  16. 后端判断用户是否关闭浏览器(关闭网站相关的全部tab)
  17. 什么是ScaleIO中的forwards rebuild和backwards rebuild?
  18. (转)SSIS_数据流转换(Union All&amp;合并联接&amp;合并)
  19. springboot自定义配置文件
  20. HDOJ.1113 Word Amalgamation(map)

热门文章

  1. BZOJ4516: [Sdoi2016]生成魔咒
  2. Express4+Mongodb超简单入门实例
  3. sql 批量插入数据到Sqlserver中 效率较高的方法
  4. 使用ab对nginx进行压力测试
  5. Java IO流学习总结
  6. 苹果iphone手机上input的button按钮颜色显示有问题,安卓却没问题
  7. hibernate学习三(使用Annotation,注解)
  8. .htaccess语法之RewriteCond与RewriteRule指令格式详细解释
  9. mysql5.x升级到mysql5.7后导入之前数据库date出错的快速解决方法【mysql低版本数据导入到高版本出错的解决方法】
  10. NoSQL指南