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: "((()))", "(()())", "(())()", "()(())", "()()()"
思路:采用递归的思想,当左括号数大于右括号数时可以加左或者右括号,否则只能加左括号,当左括号数达到n时,剩下全部。不过,每一个方法的调用都会产生一个栈帧,每执行一个方法就会出现压栈操作,所以采用递归的时候产生的栈帧比较多,递归就会影响到内存,非常消耗内存。当左括号数大于右括号数时可以加左或者右括号,否则只能加左括号,当左括号数达到n时,剩下全部。
public static void main(String[] args){
ArrayList<String> res = new ArrayList<String>();
Scanner input = new Scanner(System.in);
System.out.println("请输入n:");
int n = input.nextInt();
generate(res, "", 0, 0, n);
System.out.println(res);
}
public static void generate(ArrayList<String> res, String tmp, int lhs, int rhs, int n){
if(lhs == n){
for(int i = 0; i < n - rhs; i++){
tmp += ")";
}
res.add(tmp);
return ;
}
generate(res, tmp + "(", lhs + 1, rhs, n);
if(lhs > rhs)
generate(res, tmp + ")", lhs, rhs + 1, n);
}
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: "((()))", "(()())", "(())()", "()(())", "()()()"的更多相关文章
- [Javascript] Write a Generator Function to Generate the Alphabet / Numbers
When you need to generate a data set, a generator function is often the correct solution. A generato ...
- [LeetCode] 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 ...
- No.022:Generate Parentheses
问题: Given n pairs of parentheses, write a function to generate all combinations of well-formed paren ...
- Generate Parentheses
Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...
- 【leetcode】 Generate Parentheses (middle)☆
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [LintCode] Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 【JAVA、C++】LeetCode 022 Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
随机推荐
- memcache常用命令
一.memcached的基本命令(安装.卸载.启动.配置相关): -p 监听的端口 -l 连接的IP地址, 默认是本机 -d start 启动memcached服务 -d restart 重起m ...
- --css 控制文字多使用省略号
--css 控制文字多使用省略号.overflowPoint{ text-overflow:ellipsis;width: 200px;overflow: hidden;}<div class= ...
- jqueryl操作dom文档实例
<include file="Public:header"/> <link rel="stylesheet" href="../Pu ...
- PHP htmlspecialchars和htmlspecialchars_decode(函数)
htmlspecialchars() 函数把一些预定义的字符转换为 HTML 实体. 函数原型:htmlspecialchars(string,quotestyle,character-set) 预定 ...
- 学习Java绝对要懂的,Java编程中最常用的几种排序算法!
今天给大家分享一下Java中几种常见的排序算法的Java代码 推荐一下我的Java学习羊君前616,中959,最后444.把数字串联起来! ,群里有免费的学习视频和项目给大家练手.大神有空时也 ...
- [leetcode-495-Teemo Attacking]
In LLP world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned ...
- tomcat7的web.xml的xml片段与注解资源的发现处理逻辑
1.metadata-complete 属性 Servlet 3.0 的部署描述文件 web.xml 的顶层标签 <web-app> 有一个 metadata-complete 属性,该属 ...
- GCD 信号量 dispatch_semaphore_t
1.GCD知识讲解 1)dispatch_semaphore_create(long value) //创建一个信号量,总量为value,value不能小于0 2)dispatch_semaphore ...
- kbengine 常见问题汇总
Q: KBEngine是什么,能用来做什么?A: KBEngine是一个通用网络游戏服务器引擎,适合绝大多数中心拓扑结构的网络游戏,包括但不限于即时和回合制MMORPG.副本类.房间类.卡牌.棋牌等. ...
- VB6之SendMessage模拟拖放事件
原文链接:http://hi.baidu.com/coo_boi/item/e1e0f5ab45bddbdd5af191df 网上找了个C++的翻一下,原文链接:http://www.cnblogs. ...