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)的更多相关文章

  1. LeetCode:39. Combination Sum(Medium)

    1. 原题链接 https://leetcode.com/problems/combination-sum/description/ 2. 题目要求 给定一个整型数组candidates[ ]和目标值 ...

  2. LeetCode:36. Valid Sudoku(Medium)

    1. 原题链接 https://leetcode.com/problems/valid-sudoku/description/ 2. 题目要求 给定一个 9✖️9 的数独,判断该数独是否合法 数独用字 ...

  3. LeetCode:43. Multiply Strings (Medium)

    1. 原题链接 https://leetcode.com/problems/multiply-strings/description/ 2. 题目要求 给定两个String类型的正整数num1.num ...

  4. LeetCode 22. Generate Parentheses(构造)

    题目大意:给n个'(' 和 ')',构造出所有的长度为2*n并且有效的(可匹配的)字符串. 题目分析:这道题不难,可以直接搜索出所有可能的字符串,然后再逐一判断是否合法即可.但是还有更好的办法,实际上 ...

  5. LeetCode:16. 3Sum Closest(Medium)

    1. 原题链接 https://leetcode.com/problems/3sum-closest/description/ 2. 题目要求 数组S = nums[n]包含n个整数,找出S中三个整数 ...

  6. LeetCode:49. Group Anagrams(Medium)

    1. 原题链接 https://leetcode.com/problems/group-anagrams/description/ 2. 题目要求 给定一个字符串数组,将数组中包含相同字母的元素放在同 ...

  7. LeetCode:20. Valid Parentheses(Easy)

    1. 原题链接 https://leetcode.com/problems/valid-parentheses/description/ 2. 题目要求 给定一个字符串s,s只包含'(', ')',  ...

  8. LeetCode:9. Palindromic Number(Medium)

    原题链接:https://leetcode.com/problems/palindrome-number/description/ 1. 题目要求:判断一个int类型整数是否是回文,空间复杂度O(1) ...

  9. [Leetcode][Python]22: Generate Parentheses

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 22: Generate Parentheseshttps://oj.leet ...

随机推荐

  1. IOS CoreLocation框架的使用(用于地理定位)

    ●  在移动互联网时代,移动app能解决用户的很多生活琐事,比如 ●  导航:去任意陌生的地方 ●  周边:找餐馆.找酒店.找银行.找电影院 ●  在上述应用中,都用到了地图和定位功能,在iOS开发中 ...

  2. 用ant打包apkbuilder找不到了的解决办法

    apkbuilder的情况下生成apk文件,其实apkbuilder是一个批处理文件,打开里面就能发现,其实他内部执行的是sdklib.jar里面的一个class,所以就知道怎么做了,很简单,我们自己 ...

  3. Python 变量交换

    # coding = utf-8 a, b = 1, 2 print 'before change' print a, b a, b = b, a print 'after change' print ...

  4. 记忆化搜索,FatMouse and Cheese

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1107 http://acm.hdu.edu.cn/showpro ...

  5. Python Day 15 递归、匿名函数、内置函数

    阅读目录 内容回顾 生成器的send方法 递归 匿名函数 内置函数 ##内容回顾 #1.带参装饰器 - 自定义 | wraps def wrap(info) def outer1(func): fro ...

  6. 【转】iOS保持界面流畅的技巧

    原文链接:iOS保持界面流畅的技巧 这篇文章会非常详细的分析 iOS 界面构建中的各种性能问题以及对应的解决思路,同时给出一个开源的微博列表实现,通过实际的代码展示如何构建流畅的交互. Index演示 ...

  7. java实现按拼音排序

    List<WaPayFileVO> list =(List<WaPayFileVO>) dao.execQueryBeanList(pagesql, params.toArra ...

  8. java读取资源文件(Properties)

    四步: java代码 //new一个读取配置文件 Properties properties=new Properties(); //获取文件路径 String path=request.getSer ...

  9. Android学习笔记_37_ListView批量加载数据和页脚设置

    1.在activity_main.xml布局文件中加入ListView控件: <RelativeLayout xmlns:android="http://schemas.android ...

  10. webapi是如何绑定参数的(How WebAPI does Parameter Binding)

    原文地址 由于工作原因,要使用ASP.NET WEBAPI(非mvc webapi),前几天时间一直很紧张,所以webapi一直将就用,今天下午好不容易有时间终于看了下,解决了自己一直疑惑的问题,在此 ...