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. OO第三单元

    OO第三单元 JML语言理论基础,应用工具链 JML语言基础 JML简介 定义: JML 是一种形式化的. 面向 JAVA 的行为接口规格语言 作用: 开展规格化设计.这样交给代码实现人员的将不是可能 ...

  2. elasticsearch的dsl查询

    测试es的dsl查询,准备数据,在插入数据的时候,如果index.type.mapping都没有,es会自动创建 一.数据的准备 curl -XPOST "http://192.168.99 ...

  3. 最短路径算法:弗洛伊德(Floyd-Warshall)算法

    一.算法介绍 Floyd-Warshall算法(英语:Floyd-Warshall algorithm),中文亦称弗洛伊德算法,是解决任意两点间的最短路径的一种算法,可以正确处理有向图或负权(但不可存 ...

  4. 页表 Page tables

    逻辑地址与物理地址的转化 页表是由页表项(PTE)组成的数组.512个PTE构成一个页表页(Page-table page). PTE中包含了物理页码(PPN physical page number ...

  5. freeswitch的docker构建过程

    概述 Docker是一个开源的应用容器引擎,可以让开发者打包应用以及依赖包到一个轻量级.可移植的容器中,并在任何安装有Docker的机器上运行. Docker 使你能够将应用程序与基础架构分开,从而可 ...

  6. Linux系统编程之进程控制(进程创建、终止、等待及替换)

    进程创建 在上一节讲解进程概念时,我们提到fork函数是从已经存在的进程中创建一个新进程.那么,系统是如何创建一个新进程的呢?这就需要我们更深入的剖析fork函数. 1.1 fork函数的返回值 调用 ...

  7. ONVIF协议客户端

    前几天跟大家聊了一些关于ONVIF的一些基础知识,它的工作原理以及优势.今天安徽思蔷信息科技为带大家了解一下simpleonvif 百度云盘下载地址:链接:https://pan.baidu.com/ ...

  8. virtualbox + vagrant 安装centos7 以及 vagrant up下载太慢的解决方案

    下载安装 virtualbox下载 vagrant下载 下载启动镜像vagrant up有下载过慢的问题,可以到网页vagrant镜像仓库,找到自己需要的镜像,选择virtualbox版本下载 下载好 ...

  9. Maven下载、安装、配置

    简介 Maven是一个项目管理工具,主要用于Java平台的项目构建.依赖管理和项目生命周期管理. 当然对于我这样的程序猿来说,最大的好处就是对jar包的管理比较方便,只需要告诉Maven需要哪些jar ...

  10. Visual Studio 2022 初探 (vs2022附激活码)

    前言 Visual Studio 2022 正式版来了.新版本带有 go-live 许可证,可供生产使用.在 Visual Studio 2019 的基础上,新版集成开发坏境提供了非常多的改进,包括对 ...