Generate Parentheses
2015-06-02 00:08
197 查看
dfs 的一个关键是termination condition
public class Solution { public ArrayList<String> generateParenthesis(int n) { ArrayList<String> res = new ArrayList<String>(); if(n<1) return res; dfs(n,n,res,new String()); return res; } private void dfs(int nl, int nr,ArrayList<String> res, String item){ if(nr<nl) return; // nr>nl not working!!! if(nr==0 && nl==0) res.add(item); if(nl>0) dfs(nl-1, nr, res, item+'('); if(nr>0) dfs(nl, nr-1, res, item+')'); } }
相关文章推荐
- JavaScript中replace()方法使用时应该注意的几点
- C++对象模型之编译器如何处理函数返回一个对象
- LeetCode解题报告--ZigZag Conversion
- 遗传算法代码
- myeclipse取消js校验
- opencv & qt study-(2)-打开video& camera
- php实现一段非常有意思的代码(可扩展)
- myeclipse取消js校验
- 爱快路由计费系统easyradius隆重发布,支持V2版本,欢迎大家测试使用
- [AUTONAVx][lec3]3D Geometry and Sensors
- jsp中include使用html静态文件出现乱码问题
- 我的代数充能01
- MidnightBSD 发布 1.0!
- Linux Shell脚本系列教程(一):Shell入门
- Linux Shell脚本系列教程(二):终端打印命令详解
- Linux Shell脚本系列教程(三):变量和环境变量
- Linux Shell脚本系列教程(四):使用函数添加环境变量
- Linux Shell脚本系列教程(五):数学运算
- Linux Shell脚本系列教程(六):数组和关联数组
- Linux Shell脚本系列教程(七):脚本调试