给 n 对括号,写一个函数生成所有合适的括号组合。
比如,给定 n = 3,一个结果为:
[
  "((()))",
  "(()())",
  "(())()",
  "()(())",
  "()()()"
]
详见:https://leetcode.com/problems/generate-parentheses/description/

由于字符串只有左括号和右括号两种字符,而且最终结果必定是左括号3个,右括号3个。定义两个变量left和right分别表示剩余左右括号的个数,如果在某次递归时,左括号的个数大于右括号的个数,说明此时生成的字符串中右括号的个数大于左括号的个数,即会出现')('这样的非法串,所以这种情况直接返回,不继续处理。如果left和right都为0,则说明此时生成的字符串已有3个左括号和3个右括号,且字符串合法,则存入结果中后返回。如果以上两种情况都不满足,若此时left大于0,则调用递归函数,注意参数的更新,若right大于0,则调用递归函数,同样要更新参数。

实现语言:Java

class Solution {
public List<String> generateParenthesis(int n) {
List<String> res=new ArrayList<String>();
generateParenthesisDFS(n,n,"",res);
return res;
}
void generateParenthesisDFS(int left,int right,String out,List<String> res){
if(left>right){
return;
}
if(left==0&&right==0){
res.add(out);
}else{
if(left>0){
generateParenthesisDFS(left-1,right,out+"(",res);
}
if(right>0){
generateParenthesisDFS(left,right-1,out+")",res);
}
}
}
}

参考:https://www.cnblogs.com/grandyang/p/4444160.html

022 Generate Parentheses 生成括号的更多相关文章

  1. [CareerCup] 9.6 Generate Parentheses 生成括号

    9.6 Implement an algorithm to print all valid (e.g., properly opened and closed) combinations of n-p ...

  2. generate parentheses(生成括号)

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  3. [LintCode] Generate Parentheses 生成括号

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  4. [LeetCode] Generate Parentheses 生成括号

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  5. [leetcode]22. Generate Parentheses生成括号

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  6. [LeetCode] 22. Generate Parentheses 生成括号

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  7. 22.Generate Parentheses[M]括号生成

    题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...

  8. LeetCode 022 Generate Parentheses

    题目描述:Generate Parentheses Given n pairs of parentheses, write a function to generate all combination ...

  9. LeetCode OJ:Generate Parentheses(括号生成)

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

随机推荐

  1. Python:sample函数

    sample(序列a,n) 功能:从序列a中随机抽取n个元素,并将n个元素生以list形式返回. 例: from random import randint, sample date = [randi ...

  2. JSP标签和EL表达式

    1.jsp标签: sun原生的,直接jsp使用 <jsp:include> -- 实现页面包含,动态包含 <jsp:include page="/index.jsp&quo ...

  3. css3 tranform perspective属性

    perspective 属性用于规定观察点距离元素的距离, 1 观察点距离元素越近,元素变形就越大,灭点距离越近. 2 观察点距离元素越远,元素变形越小,灭点距离也就越远. 比如设置perspecti ...

  4. pythoon_interview_redit

    easy/intermediate What are Python decorators and how would you use them?How would you setup many pro ...

  5. 问题:sqlserver if;结果: SqlServer if else和case

    SqlServer if else和case 分类: SQLSERVER 2013-03-01 16:51 11328人阅读 评论(0) 收藏 举报 行转列 目录(?)[+] if else 要提示的 ...

  6. HTML input 标签不可编辑的 readonly 属性

    1 <form action="form_action.asp" method="get"> Name:<input type="t ...

  7. Tiny4412学习杂记

    1.Android 挂载NFS 使用 busybox mount 来替代mount命令 2.修改Uboot中fastboot最大buff  使用U-boot烧写Android5.0的时候出现 remo ...

  8. Ajax的包装

    /** * Created by Administrator on 2016/12/27. *//** * 创建XMLHttpRequest对象 * @param _method 请求方式: post ...

  9. 十道海量数据处理面试题 - 数据分析与数据挖掘技术-炼数成金-Dataguru专业数据分析社区

    1.海量日志数据,提取出某日访问百度次数最多的那个IP. 首先是这一天,并且是访问百度的日志中的IP取出来,逐个写入到一个大文件中.注意到IP是32位的,最多有个2^32个IP.同样可以采用映射的方法 ...

  10. 9、perldoc文档阅读器

    转载:http://www.cnblogs.com/nkwy2012/p/6016320.html 一般来说,将文档的名称作为参数传递给perldoc命令,即可查阅该文档.比如下面,给定文档名称per ...