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 ...
随机推荐
- DeepQA websocket 并发测试
var client = new Array(); var W3CWebSocket = new Array(); var concurrent = 2; for (var i = 0; i < ...
- vue.js--基础 事件结合双向数据绑定实现todolist,增加和删除功能
原理很简单,写一个input框,定义一个空的list,当在input中增加数据时,就往list中添加数据,然后在循环这个list的数据,删除数据就是调用list中的splice <templat ...
- Android开发学习之TabView选项卡具体解释 -- 基于Android4.4
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/he90227/article/details/24474197 直接上代码 -- 基于Android ...
- 【转】总结oninput、onchange与onpropertychange事件的用法和区别
经本人测试在chrome下的从历史记录中选取值的时候也户触发input事件 前端页面开发的很多情况下都需要实时监听文本框输入,比如腾讯微博编写140字的微博时输入框hu9i动态显示还可以输入的字数.过 ...
- 去掉video视频播放器下的下载按钮
去掉video视频播放器下的下载按钮: video::-internal-media-controls-download-button { display:none; } video::-webkit ...
- 【洛谷P3205】[HNOI2010]CHORUS 合唱队
合唱队 区间DP f[l][r][0/1]表示生成目标序列中的区间[l,r],最后一个数是a[l]/a[r] 的方案数 边界: f[i][i][0]=1 转移: f[l][r][0]=(a[l]< ...
- HDU 1226 超级密码(数学 bfs)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1226 超级密码 Time Limit: 20000/10000 MS (Java/Others) ...
- Code First 一
Code-First和我们的数据库优先方式是相反的,数据库优先是通过数据库映射出相应的类和上下文,Code-First测试通过创建的类和上下文得到相应的数据库. Code-First主要用于领域驱动设 ...
- Oracle-01-数据库分类/oracle sql*plus常用命令
一.数据库分类 一.数据库分类1.小型数据库:access.foxbase2.中型数据库:informix.sql server.mysql3.大型数据库:sybase.db2.oracle 二.项目 ...
- rest_framework --- viewsets
viewsets :from rest_framework import viewsets #导入方式 ViewSetMixin(object): 这个类,大致作用就是重写了as_view()方法,假 ...