1. Generate Parentheses My Submissions QuestionEditorial Solution

    Total Accepted: 86957 Total Submissions: 234754 Difficulty: Medium

    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:

“((()))”, “(()())”, “(())()”, “()(())”, “()()()”

思路:

f(1)="()"
f(2)="("+f(1)+")"||"()"+f(1)||f(1)+"()"
...
f(n)="("+f(n−1)+")"||f(1)f(n−1)||f(2)f(n−2)||...||f(n−1)f(1)

注意,这其中计算时每次可能都会有重复,所以自底向上迭代时,在底层依次去重,可以节省大量内存空间,内层循环次数也会减少。

class Solution {
public:
vector<string> generateParenthesis(int n) {
vector<vector<string>> res(n+1);
res[1].push_back("()");
for(int i=2;i<=n;++i){ //有i组括号的时候,自底向上迭代
vector<string> vs;
for(int j=1;j<i;++j){ //f(j)f(i-j)的结果组合起来
for(int p=0;p<res[j].size();++p){
for(int q=0;q<res[i-j].size();++q){
vs.push_back(res[j][p]+res[i-j][q]);
}
if(j==i-1)vs.push_back("("+res[j][p]+")");
}
}
sort(vs.begin(),vs.end());
vs.erase(unique(vs.begin(),vs.end()),vs.end());
res[i] = vs;
}
sort(res[n].begin(),res[n].end());
res[n].erase(unique(res[n].begin(),res[n].end()),res[n].end());
return res[n];
}
};

47-Generate Parentheses的更多相关文章

  1. 72. Generate Parentheses && Valid Parentheses

    Generate Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...

  2. Generate Parentheses

    Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...

  3. [LintCode] Generate Parentheses 生成括号

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

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

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

  5. 【题解】【排列组合】【回溯】【Leetcode】Generate Parentheses

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

  6. leetcode022. Generate Parentheses

    leetcode 022. Generate Parentheses Concise recursive C++ solution class Solution { public: vector< ...

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

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

  8. N-Queens And N-Queens II [LeetCode] + Generate Parentheses[LeetCode] + 回溯法

    回溯法 百度百科:回溯法(探索与回溯法)是一种选优搜索法,按选优条件向前搜索,以达到目标.但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步又一次选择,这样的走不通就退回再走的技术为回溯法 ...

  9. 22. Generate Parentheses(ML)

    22. Generate Parentheses . Generate Parentheses Given n pairs of parentheses, write a function to ge ...

  10. leetcode-algorithms-22 Generate Parentheses

    leetcode-algorithms-22 Generate Parentheses Given n pairs of parentheses, write a function to genera ...

随机推荐

  1. 2021.9.18考试总结[NOIP模拟56]

    T1 爆零 贪心地想,肯定要先走完整个子树再走下一个,且要尽量晚地走深度大的叶子.所以对每个点的儿子以子树树高为关键字排序$DFS$即可. 也可$DP$. $code:$ T1 #include< ...

  2. WiFi天线对PCB布局布线和结构的要求详解 - 全文

    随着市场竞争的加剧,硬件设备正以集成化的方向发展.天线也由外置进化内置再进化到嵌入式,我们先来介绍这类应用的天线种类: ⑴ On Board板载式:采用PCB蚀刻一体成型,性能受限,极低成本,应用于蓝 ...

  3. stm32看门狗详细解答,看了觉得一下子明白了很多

    一.独立看门狗 STM32 的独立看门狗由内部专门的 40Khz 低速时钟驱动,即使主时钟发生故障,它也仍然有效. 看门狗的原理:单片机系统在外界的干扰下会出现程序跑飞的现象导致出现死循环,看门狗电路 ...

  4. Linux线程互斥学习笔记--详细分析

    一.互斥锁 为啥要有互斥? 多个进程/线程执行的先后顺序不确定,何时切出CPU也不确定. 多个进程/线程访问变量的动作往往不是原子的. 1. 操作步骤 (1)创建锁 // 创建互斥锁mutex pth ...

  5. IPv6(诞生原因、数据报格式、与IPv4的不同、地址表现形式、基本地址类型、IPv6与IPv4的过渡策略)

    文章转自:https://blog.csdn.net/weixin_43914604/article/details/105297642 学习课程:<2019王道考研计算机网络> 学习目的 ...

  6. Python课程笔记(一)

    由于新冠状病毒的爆发,不得不在家里上网课,开课已经两个礼拜了,今天上完Python课后,准备整理一下最近学习Python的笔记. 人生苦短,我用Python 一.Hello World 初学一门新的语 ...

  7. 精准测试系列分享之一:JaCoCo 企业级应用的优缺点分析

    一.JaCoCo简介 JaCoCo是Eclipse平台下的开源产品,以小型,轻量化著称,常见集成在Eclipse Workbench中,除此之外的启动方式包括对接Ant和Maven,或是命令行的方式进 ...

  8. jenkins 生成HTML报表,邮件推送

    1.登录jenkins,系统管理=>插件管理 =>可选插件安装 安装成功: 2.打开任务,进入配置 3.添加构建后操作 4.配置页面 5.构建后report输出配置完成后点击立即构建,构建 ...

  9. Go语言并发模型 G源码分析

    Go 的线程实现模型,有三个核心的元素 M.P.G,它们共同支撑起了这个线程模型的框架.其中,G 是 goroutine 的缩写,通常称为 "协程".关于协程.线程和进程三者的异同 ...

  10. [python]Pytest+selenium+git+jenkins持续集成

    1安装pytest框架 &pip install pytest   #pytest &pip install pytest-html  #pytest html测试报告 2.工程介绍 ...