LintCode-生成括号
来源:互联网 发布:linux系统查看ip地址 编辑:程序博客网 时间:2024/06/09 05:36
生成括号
给定 n 对括号,请写一个函数以将其生成新的括号组合,并返回所有组合结果。
样例
给定 n = 3, 可生成的组合如下:
“((()))”, “(()())”, “(())()”, “()(())”, “()()()”
public class Solution { /** * @param n n pairs * @return All combinations of well-formed parentheses */public ArrayList<String> generateParenthesis(int n) { ArrayList<String> result = new ArrayList<String>(); String str = new String(); findPaths(0, 0, n, str, result); return result; } public void findPaths(int n, int m, int sum, String str, List<String> result) { String str0 = new String(); String str1 = new String(); if (sum - n > 0) { str0 = str + "("; this.findPaths(n + 1, m + 1, sum, str0, result); } if (m > 0) { str1 = str + ")"; this.findPaths(n, m - 1, sum, str1, result); } if(sum==n&&m==0) result.add(str); }}
0 0
- LintCode : 生成括号
- LintCode-生成括号
- LintCode:生成括号
- LintCode:生成括号
- lintcode,生成括号
- 生成括号-lintcode
- lintcode -- 生成括号
- LintCode 生成括号
- DFS-lintcode生成括号
- 生成括号-LintCode
- lintcode刷题--生成括号
- lintcode--生成括号
- [Lintcode]Generate Parentheses 生成括号
- Generate Parentheses 生成括号-- LintCode题解
- lintcode括号匹配
- 生成括号
- 生成括号
- 生成括号
- 看JDBC轻松连接数据库,你值得拥有
- 前端面试题
- 构造哈弗曼树
- LightOJ 1292 Laser Shot(暴力)
- Mybatis入门实例(1) - SSM(一)
- LintCode-生成括号
- 005_Http之request获取客户端信息06-请求转发及包含和重定向的区别
- 项目中遇到的JS闭包问题
- Android数据库更新并保留原来数据的实现
- VB.net调用蒙恬Write2Go笔迹手写板进行电子签名
- 记录看到的一些jquery效率上的问题
- css学习(一)-CSS基础、css选择器
- lucky string
- Codeforces 629D Babaei and Birthday Cake 【线段树优化dp】