LeetCode:22. Generate Parentheses(Medium)
1. 原题链接
https://leetcode.com/problems/generate-parentheses/description/
2. 题目要求
给出一个正整数n,请求出由n对合法的圆括号组合
例如,n = 3,答案:

3. 解题思路
采用递归的方法:给定的整数为n,定义一个字符串类型变量str用来保存组合。"("的个数记为left,")"的个数记为right,当left<n时或者right<left时都进行递归。
当str的长度length==2*n时,说明该组合已由n对圆括号组成,返回。
4. 代码实现
import java.util.ArrayList;
import java.util.List; public class GenerateParentheses22 {
public static void main(String[] args) {
List<String> res = new ArrayList<>();
res = GenerateParentheses22.generateParenthesis(4);
for (String s : res)
System.out.println(s);
} public static List<String> generateParenthesis(int n) {
List<String> res = new ArrayList<>();
backtrack(res, "", 0, 0, n);
return res; } public static void backtrack(List<String> res, String str, int left, int right, int n) {
if (str.length() == n * 2) {
res.add(str);
return; // 直接跳出该次递归
} if (left < n)
backtrack(res, str + "(", left + 1, right, n);
if (right < left)
backtrack(res, str + ")", left, right + 1, n);
}
}
运行结果:

2017-12-27 09:47:49
LeetCode:22. Generate Parentheses(Medium)的更多相关文章
- LeetCode:39. Combination Sum(Medium)
1. 原题链接 https://leetcode.com/problems/combination-sum/description/ 2. 题目要求 给定一个整型数组candidates[ ]和目标值 ...
- LeetCode:36. Valid Sudoku(Medium)
1. 原题链接 https://leetcode.com/problems/valid-sudoku/description/ 2. 题目要求 给定一个 9✖️9 的数独,判断该数独是否合法 数独用字 ...
- LeetCode:43. Multiply Strings (Medium)
1. 原题链接 https://leetcode.com/problems/multiply-strings/description/ 2. 题目要求 给定两个String类型的正整数num1.num ...
- LeetCode 22. Generate Parentheses(构造)
题目大意:给n个'(' 和 ')',构造出所有的长度为2*n并且有效的(可匹配的)字符串. 题目分析:这道题不难,可以直接搜索出所有可能的字符串,然后再逐一判断是否合法即可.但是还有更好的办法,实际上 ...
- LeetCode:16. 3Sum Closest(Medium)
1. 原题链接 https://leetcode.com/problems/3sum-closest/description/ 2. 题目要求 数组S = nums[n]包含n个整数,找出S中三个整数 ...
- LeetCode:49. Group Anagrams(Medium)
1. 原题链接 https://leetcode.com/problems/group-anagrams/description/ 2. 题目要求 给定一个字符串数组,将数组中包含相同字母的元素放在同 ...
- LeetCode:20. Valid Parentheses(Easy)
1. 原题链接 https://leetcode.com/problems/valid-parentheses/description/ 2. 题目要求 给定一个字符串s,s只包含'(', ')', ...
- LeetCode:9. Palindromic Number(Medium)
原题链接:https://leetcode.com/problems/palindrome-number/description/ 1. 题目要求:判断一个int类型整数是否是回文,空间复杂度O(1) ...
- [Leetcode][Python]22: Generate Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 22: Generate Parentheseshttps://oj.leet ...
随机推荐
- IOS KVO的实现原理
#import "HMViewController.h" #import "HMPerson.h" @interface HMViewController () ...
- HDU-3092 Least common multiple---数论+分组背包
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3092 题目大意: 有一个数字n,现在要把它分解成几个数字相加!然后这几个数字有最小公倍数,题目目的是 ...
- POJ 1470 Closest Common Ancestors 【LCA】
任意门:http://poj.org/problem?id=1470 Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000 ...
- Spring data jpa命名规范
JPA命名规范 (sample与JPQL等效) Table 4. Supported keywords inside method names Keyword Sample JPQL snippet ...
- 【题解】洛谷P1262 间谍网络 (强连通分量缩点)
洛谷P1262:https://www.luogu.org/problemnew/show/P1262 思路 一看题目就知道是强连通分量缩点 当图中有强连通分量时 将其缩点 我们可以用dfn数组判断是 ...
- PLSQL 禁用所有约束,启用约束,索引,触发器等
--禁用外键和触发器 SET SERVEROUTPUT ON SIZE 50000BEGINfor c in (select 'ALTER TABLE '||TABLE_NAME||' DISABLE ...
- vue富文本编辑,编辑自动预览,单个图片上传不能预览的问题解决:
//预览<div class="htmlViewBox"> <p v-html="activity_html_defaultMsg" v-sh ...
- 【oracle使用笔记2】使用Oracle数据库遇到的若干问题总结
一. 关于Oracle 11g数据库在查询表中数据显示中文乱码问题 [描述]本人一开始使用的Oracle是11g版本的,用PLSQL一次查询表中的数据时出现了中文显示乱码,为此搜了许多解决办法,最终通 ...
- 没有美工一样可以获取设计各种各样的UI图
没有美工一样可以获取设计各种各样的UI图 http://www.iconfont.cn
- 网页后缀html、htm、shtml、shtm有什么区别?
每一个网页或者说是web页都有其固定的后缀名,不同的后缀名对应着不同的文件格式和不同的规则.协议.用法,最常见的web页的后缀名是.html和.htm,但这只是web页最基本的两种文件格式,今天我们来 ...