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. BUAA 软工 | 从计算机技术中探索艺术之路

    项目 内容 这个作业属于哪个课程 2020春季计算机学院软件工程(罗杰 任健) 这个作业的要求在哪里 第一次作业-热身! 我在这个课程的目标是 掌握软件开发方法学和工程学知识 这个作业在哪个具体方面帮 ...

  2. FastAPI 学习之路(三十四)数据库多表操作

    之前我们分享的是基于单个的数据库表的操作,我们在设计数据库的时候也设计了跨表,我们可以看下数据库的设计. class User(Base): __tablename__ = "users&q ...

  3. openmp学习心得(一)

    主要在vs2015下使用OMP,写一些自己omp的学习心得: 一.在VS2015下OpenMP的使用: 1.VS2015也仅仅支持OpenMP2.0版本,VS对OpenMP的支持并不太好. 2.在VS ...

  4. 基于Vue的工作流项目模块中,使用动态组件的方式统一呈现不同表单数据的处理方式

    在基于Vue的工作流项目模块中,我们在查看表单明细的时候,需要包含公用表单信息,特定表单信息两部分内容.前者表单数据可以统一呈现,而后者则是不同业务的表单数据不同.为了实现更好的维护性,把它们分开作为 ...

  5. HBase的安装与部署

    一.部署前置环境 先部署分布式的高可用版的Hadoop,即ZooKeeper+Hadoop. https://www.cnblogs.com/live41/p/15483192.html * 部署的服 ...

  6. vs2015 MSB600 "inf2cat.ext"已退出,代码为2

    使用vs2015编译XDMA驱动过程中,报如下错误: vs2015 MSB600 "inf2cat.ext"已退出,代码为2 在使用Qt编译PCIE码表的过程中,出现C1038:无 ...

  7. redis 的主从模式哨兵模式

    原理理解 1,哨兵的作用就是检测redis主服务的状态,如果主服务器挂了,从服务就自动切换为主服务器,变为master.哨兵是一个独立的进程,作为进程,它会独立运行.其原理是哨兵通过发送命令,等待Re ...

  8. Oracle 整库备份还原

    http://www.mamicode.com/info-detail-2481866.html sql语句 system用户登陆 查看表空间和存放位置 select t1.name,t2.name ...

  9. java 三大特性_继承、封装、多态_day005

    一.继承: java的三大特性之一.两个类之间通过extends关键字来描述父子关系,子类便可拥有父类的公共方法和公共属性.子类可以继承父类的方法和属性,子类也可以自己定义没有的方法或者通过覆盖父类的 ...

  10. MySQL——DML数据增、删、改

    插入 方式一(经典) 语法: insert into 表名(列名,...) values(值,...); 若要一次插入多行 create table if NOT EXISTS user( name ...