• 作者: 负雪明烛
  • id: fuxuemingzhu
  • 个人博客:http://fuxuemingzhu.cn/
  • 个人公众号:负雪明烛
  • 本文关键词:括号, 括号生成,题解,leetcode, 力扣,Python, C++, Java

题目地址:https://leetcode.com/problems/generate-parentheses/description/

题目描述

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:

[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]

题目大意

给定n,要输出n个左括号和右括号能匹配的所有字符串。

解题方法

回溯法

仍然是回溯法的题目,回溯法的题目可以看看这个文章:
https://segmentfault.com/a/1190000006121957

判断回溯很简单,拿到一个问题,你感觉如果不穷举一下就没法知道答案,那就可以开始回溯了。

一般回溯的问题有三种:

  1. Find a path to success 有没有解
  2. Find all paths to success 求所有解
    • 求所有解的个数
    • 求所有解的具体信息
  3. Find the best path to success 求最优解

在我的理解中,回溯法是一个剪枝了的二叉树。我们要得到的结果是可以good leaf,如果不满足good leaf就继续向下搜索,搜索的时候需要满足一定的条件。

从上面的图片中我们可以很明显的看到,最后五条画黑线的就是最终的结果,其中左分支都是添加左括号,又分支都是添加右括号。

那么我们在什么情况下添加左括号呢?很明显,最多能添加 n 个左括号,在递归调用的时候,在能传递到最底层的共用字符串中先添加 ”(“ ,然后 left-1,递归调用就可以。

那什么时候添加右括号呢?当左括号个数大于右括号的个数时添加右括号。

总之,向下搜索要满足两个条件:

  1. 插入数量不超过n
  2. 可以插入 的前提是 ( 的数量大于

代码后面的判断条件都是if,而不是elif,因为是满足两个条件的任意一个就可以继续向下搜索,而不是同时只能满足其中的一个。

class Solution(object):
def generateParenthesis(self, n):
"""
:type n: int
:rtype: List[str]
"""
res = []
self.dfs(res, n, n, '')
return res def dfs(self, res, left, right, path):
if left == 0 and right == 0:
res.append(path)
return
if left > 0:
self.dfs(res, left - 1, right, path + '(')
if left < right:
self.dfs(res, left, right - 1, path + ')')

二刷,使用C++,基本结构和上面一样,不过这里lc和rc分别表示左括号的个数和右括号的个数。vector的push_back()方法调用的时候实际上是使用的值传递,也就是会进行赋值到vector里。

class Solution {
public:
vector<string> generateParenthesis(int n) {
vector<string> res;
int lc = 0, rc = 0;
dfs(res, "", n, lc, rc);
return res;
}
void dfs(vector<string>& res, string path, int n, int lc, int rc) {
if (rc > lc || lc > n || rc > n) return;
if (lc == rc && lc == n) {
res.push_back(path);
return;
}
dfs(res, path + '(', n, lc + 1, rc);
dfs(res, path + ')', n, lc, rc + 1);
}
};

日期

2018 年 2 月 24 日
2018 年 12 月 14 日 —— 12月过半,2019就要开始
2020 年 4 月 9 日 —— 天气好暖和呀

【LeetCode】22. Generate Parentheses 括号生成的更多相关文章

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

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

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

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

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

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

  4. LeetCode 22 Generate Parentheses(找到所有匹配的括号组合)

    题目链接 : https://leetcode.com/problems/generate-parentheses/?tab=Description   给一个整数n,找到所有合法的 () pairs ...

  5. leetcode@ [22]Generate Parentheses (递归 + 卡特兰数)

    https://leetcode.com/problems/generate-parentheses/ Given n pairs of parentheses, write a function t ...

  6. 蜗牛慢慢爬 LeetCode 22. Generate Parentheses [Difficulty: Medium]

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

  7. [LeetCode] 22. Generate Parentheses ☆☆

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

  8. LeetCode 22. Generate Parentheses

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

  9. Java [leetcode 22]Generate Parentheses

    题目描述: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...

随机推荐

  1. nohup使用

    nohup:不挂断运行 在忽略挂起信号的情况下运行给定的命令,以便在注销后命令可以在后台继续运行. 可以这么理解:不挂断的运行,注意并没有后台运行的功能,就是指,用nohup 运行命令可以是命令永远运 ...

  2. Python基础之流程控制while循环

    目录 1. 语法 2. while+break 3. while+continue 4. while+else 1. 语法 最简单的while循环如下: ''' while <条件>: & ...

  3. Google服务器架构图解简析

    无疑是互联网时代最闪亮的明星.截止到今天为止,Google美国主站在Alexa排名已经连续3年第一,Alexa Top100中,各国的Google分站竟然霸占了超过20多个名额,不得不令人感叹Goog ...

  4. Linux修改默认挂载NFS协议版本

    系统版本:CentOS Linux release 7.4.1708 (Core) $ vi /etc/nfsmount.conf # # /etc/nfsmount.conf - see nfsmo ...

  5. GISer如何突破二次开发瓶颈

    年初时写的<一个GISer的使命>那篇文章中,提出了GISer的技术提升路径可以分为四个大的阶段: 阶段一,能使用商业GIS软件去解决问题. 阶段二,能使用开源GIS软件去解决问题. 阶段 ...

  6. 扩展kmp 学习笔记

    学习了一下这个较为冷门的知识,由于从日报开始看起,还是比较绕的-- 首先定义 \(Z\) 函数表示后缀 \(i\) 与整个串的 \(lcp\) 长度 一个比较好的理解于实现方式是类似于 \(manac ...

  7. 源码分析-Producer

    消息生产者的代码都在client模块中,相对于RocketMQ来讲,消息生产者就是客户端,也是消息的提供者. 方法和属性 主要方法介绍 //创建主题 void createTopic(final St ...

  8. 轻松理解webpack热更新原理

    一.前言 - webpack热更新 Hot Module Replacement,简称HMR,无需完全刷新整个页面的同时,更新模块.HMR的好处,在日常开发工作中体会颇深:节省宝贵的开发时间.提升开发 ...

  9. 转 Android Studio中Junit调试

    转:https://blog.csdn.net/xanthus_li/article/details/54314189 在程序开发完成后,需要交给专业的调试人员进行相关的专业调试(白盒测试,黑盒测试, ...

  10. linux 加密安全之AWK

    密钥 密钥一般是一串字符串或数字,在加密或者解密时传递给加密或者解密算法,以使算法能够正确对明文加密或密文解密. 加密算法 已知的加密算法有对称和非对称加密,也就是说你想进行加解密操作的时候需要具备密 ...