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 ...
随机推荐
- 虚拟机安装Centos6.5之后的网络配置
我使用的是minimal模式安装的,默认是无法联网的,需要自己配置,下面我列举2种联网的配置方法 方法1: 默认使用的是NAT模式,修改/etc/sysconfig/network-scripts/i ...
- Custom Control
How to create custom control http://www.silverlightshow.net/items/Creating-a-Silverlight-Custom-Cont ...
- ENVI中利用polygon掩膜修改类到指定类
overlay——classification——制定分类的图像 edit——polygon delete from class(选择这个掩膜模式) edit——set delete class va ...
- sybase下convert函数第三个参数(时间格式)
convert(varchar(10),字段名,转换格式) 比如:1.select user_id,convert(varchar(10),dayts,11) as dates from tb_use ...
- MVC5+EF6+BootStrap3.3.5 博客系统之项目搭建(一)
环境:vs2013,sql2008R2 引用版本:MVC5,EF6,BootStrap3.3.5 在之前一直都是webfrom开发,虽然开发简单:但是有很多不足的地方.在之前开发都是webfrom+M ...
- ios 中的小技巧 - 总有你想要的 一
UITableView的Group样式下顶部空白处理 在viewWillAppear里面添加如下代码: //分组列表头部空白处理 CGRect frame = myTableView.tableHea ...
- UITextField监听文字输入事件
[textField addTarget:self action:@selector(textFieldDidChange:)forControlEvents:UIControlEventEditin ...
- iOS基本网络请求
常见的网络请求有同步GET, 同步POST, 异步GET, 异步POST. GET请求和POST请求的区别: 1. GET请求的接口会包含参数部分,参数会作为网址的一部分,服务器地址与参数之间通过 ? ...
- Android中Linux suspend/resume流程
Android中Linux suspend/resume流程首先我们从linux kernel 的suspend说起,不管你是使用echo mem > /sys/power/state 或者使用 ...
- 使用Docker解决同一服务器运行不同版本PHP方案。
前言: 最近公司有两个站点,分别是两种系统进行二次开发,基于LNMP架构的网站.一般想PHP这种非编译型语言想要对外出售源码都会进行加密,加密方法有很多种,大部分都是使用Zend Guard来进行加密 ...