LeetCode——Generate Parentheses
Description:
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:
"((()))", "(()())", "(())()", "()(())", "()()()"
参考:http://blog.csdn.net/yutianzuijin/article/details/13161721
public class Solution {
public void solve(int left, int right, String ans, List<String> res) {
if(left == 0 && right == 0) {
res.add(ans);
}
if(left > 0) {
solve(left-1, right, ans+"(", res);
}
if(right>0 && left<right) {
solve(left, right-1, ans+")", res);
}
}
public List<String> generateParenthesis(int n) {
List<String> list = new ArrayList<String>();
String ans = new String();
solve(n, n, ans, list);
return list;
}
}
LeetCode——Generate Parentheses的更多相关文章
- N-Queens And N-Queens II [LeetCode] + Generate Parentheses[LeetCode] + 回溯法
回溯法 百度百科:回溯法(探索与回溯法)是一种选优搜索法,按选优条件向前搜索,以达到目标.但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步又一次选择,这样的走不通就退回再走的技术为回溯法 ...
- LeetCode: Generate Parentheses 解题报告
Generate ParenthesesGiven n pairs of parentheses, write a function to generate all combinations of w ...
- [LeetCode]Generate Parentheses题解
Generate Parentheses: Given n pairs of parentheses, write a function to generate all combinations of ...
- [LeetCode] Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- LeetCode Generate Parentheses (DFS)
题意 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- LeetCode: Generate Parentheses [021]
[称号] Given n pairs of parentheses, write a function to generate all combinations of well-formed pare ...
- LeetCode Generate Parentheses 构造括号串(DFS简单题)
题意: 产生n对合法括号的所有组合,用vector<string>返回. 思路: 递归和迭代都可以产生.复杂度都可以为O(2n*合法的括号组合数),即每次产生出的括号序列都保证是合法的. ...
- leetcode Generate Parentheses python
# 解题思路:列举出所有合法的括号匹配,使用dfs.如果左括号的数量大于右括号的数量的话,就不能产生合法的括号匹配class Solution(object): def generateParenth ...
- 并没有看起来那么简单leetcode Generate Parentheses
问题解法参考 它给出了这个问题的探讨. 超时的代码: 这个当n等于7时,已经要很长时间出结果了.这个算法的复杂度是O(n^2). #include<iostream> #include&l ...
随机推荐
- netfiler/iptables
一. 什么是netfilter netfilter is a set of hooks inside the Linux kernel that allows kernel modules to re ...
- 路由表及route使用
路由表 linux下通过route可以查看本地路由表: Kernel IP routing table Destination Gateway Genmask ...
- springMVC下的javascript调试
最近想弄一个hadoop的管理界面,所以在网上下了一个名为jeecg的快速开发平台,由于工作之后没有用过java做网站,遇到了好多小问题,其中一个就是现在要说的javascript脚本调试的问题.说来 ...
- Linux - tar命令 压缩 和 解压
压缩 tar -cvf jpg.tar *.jpg //将目录里所有jpg文件打包成tar.jpg tar -czf jpg.tar.gz *.jpg //将目录里所有jpg文件打包成jpg.tar后 ...
- Message Code 【27796】 Failed to connect to server 'hostname';port_ld': 'reason'.
Message Code [27796] Failed to connect to server 'hostname';port_ld': 'reason'.Unable to connect to ...
- 关于Cocos2d-x中精灵节点的透明度的设置
1.当我们需要某个精灵作为一个大一点的容器来存放其他的小精灵的时候,我们先设置这个精灵的大小 setTextureRect(Rect(0, 0, babySize.width, babySize.he ...
- struts2中struts.xml和web.xml文件解析及工作原理
web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp ...
- Spring 4 官方文档学习(五)核心技术之SpEL
题外话 官方文档用evaluate这个单词来描述从表达式中获得实际内容的过程.如果直译的话,应该是评估.估值之类的意思.个人以为翻译成解析更易懂,但parse已经是解析了,为了避免冲突,就只好保留了e ...
- enumerate遍历列表
enumerate 函数用于遍历序列中的元素以及它们的下标: >>> for i,j in enumerate(('a','b','c')): print i,j 0 a 1 b ...
- 搜集的一些酷炫的金属色 ,RGB值 和大家分享一下
开发iOS程序过程中会使用到RGB,要注意每个RGB值都要除以 255.0 ,注意: ' .0 ' 不能省!! 一下是本人搜集的一些酷炫金属色的RGB值: 黄金 242,192,86 石墨 87, ...