Java [leetcode 22]Generate Parentheses
题目描述:
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:
"((()))", "(()())", "(())()", "()(())", "()()()"
解题思路:
利用递归的思想。用left和right分别记录剩余的左,右括号数。
每次都首先把左括号放进字符串中。
放入右括号的条件是:右括号剩余的数目大于0,且左括号剩余数目小于右括号剩余数目。
当left和right都为0时表明字符串已形成,加入到List中。
代码如下:
- public class Solution {
- List<String> list = new ArrayList<String>();
- public List<String> generateParenthesis(int n) {
- if (n <= 0)
- return list;
- generate("", n, n);
- return list;
- }
- public void generate(String s, int left, int right) {
- if (left == 0 && right == 0) {
- list.add(s);
- return;
- }
- if (left > 0)
- generate(s + '(', left - 1, right);
- if (right > 0 && right > left)
- generate(s + ')', left, right - 1);
- }
- }
Java [leetcode 22]Generate Parentheses的更多相关文章
- [LeetCode] 22. Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- leetcode@ [22]Generate Parentheses (递归 + 卡特兰数)
https://leetcode.com/problems/generate-parentheses/ Given n pairs of parentheses, write a function t ...
- 蜗牛慢慢爬 LeetCode 22. Generate Parentheses [Difficulty: Medium]
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- LeetCode 22. 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 22 Generate Parentheses(找到所有匹配的括号组合)
题目链接 : https://leetcode.com/problems/generate-parentheses/?tab=Description 给一个整数n,找到所有合法的 () pairs ...
- [LeetCode] 22. 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] 22. Generate Parentheses(medium)
原题 思路: 利用DFS,搜索每一种情况,同时先加"("后加")",保证()匹配正确. 最近开始学习前端,尝试用js来写. const generate = f ...
随机推荐
- NEV_SDK开发环境部署手册
根据项目开发需求,要在MEC服务器上部署如下内容:Nginx.Nginx push stream module.Jason CPP.Spawn-fcgi.libfcgi.Redis.Hiredis.B ...
- 找不到 com.google.zxing.ResultMetadataType 异常解决
在 https://github.com/zxing/zxing 下载二维码扫描 将 android 导入,code打成jar包运行时 报 06-14 23:43:08.690: E/AndroidR ...
- C# asp Aspose.Cells 教程,包含增加勾选框,单元格,字体设置
1,引用Aspose.Cells dll 2,using Aspose.Cells; 3, Workbook excel = new Workbook(); string strFilePath = ...
- mysql mysqldump只导出表结构或只导出数据的实现方法
mysql mysqldump只导出表结构或只导出数据的实现方法,需要的朋友可以参考下. mysql mysqldump 只导出表结构 不导出数据 复制代码代码如下: mysqldump --opt ...
- [译] ASP.NET 生命周期 – ASP.NET 请求生命周期(四)
不使用特殊方法来处理请求生命周期事件 HttpApplication 类是全局应用类的基类,定义了可以直接使用的一般 C# 事件.那么使用标准 C# 事件还是特殊方法那就是个人偏好的问题了,如果喜欢, ...
- Unity3D游戏开发——Asset Server搭建
本系列文章由 Amazonzx 编写,欢迎转载,转载请注明出处. http://blog.csdn.net/amazonzx/article/details/7980117 Asset Server是 ...
- 【BZOJ 3098】 Hash Killer II
Description 这天天气不错,hzhwcmhf神犇给VFleaKing出了一道题:给你一个长度为N的字符串S,求有多少个不同的长度为L的子串.子串的定义是S[l].S[l + 1].... S ...
- php文件上传大小限制的修改方法大全
php文件上传大小限制的修改方法大全 基本就是修改maxsize选项,当然为了提高上传文件的成功率,还需要设置超时时间等. 文章如下: [php文件上传]php文件上传大小限制修改,phpmyadmi ...
- 归档 NSKeyedArchiver
复杂对象无法象 NSString,NSArray等简单对象一样直接通过 writeToFile 实现持久化,当对复杂对象进行持久化时需要将其转化为 NSData (归档),但获取数据时,将 NSDat ...
- vbe6ext.olb不能被加载 宏内存溢出
今天想玩一下PowerPoint的宏,却发现玩不起来!!! 另外,每次打开ppt时都会提示vbe6ext.olb不能加载. 网上说重新下载个vbe6ext.olb然后复制到相应的路径.我也试着下载,然 ...