47-Generate Parentheses
- 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:
“((()))”, “(()())”, “(())()”, “()(())”, “()()()”
思路:
注意,这其中计算时每次可能都会有重复,所以自底向上迭代时,在底层依次去重,可以节省大量内存空间,内层循环次数也会减少。
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的更多相关文章
- 72. Generate Parentheses && Valid Parentheses
Generate Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
- Generate Parentheses
Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...
- [LintCode] Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [CareerCup] 9.6 Generate Parentheses 生成括号
9.6 Implement an algorithm to print all valid (e.g., properly opened and closed) combinations of n-p ...
- 【题解】【排列组合】【回溯】【Leetcode】Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- leetcode022. Generate Parentheses
leetcode 022. Generate Parentheses Concise recursive C++ solution class Solution { public: vector< ...
- [Leetcode][Python]22: Generate Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 22: Generate Parentheseshttps://oj.leet ...
- N-Queens And N-Queens II [LeetCode] + Generate Parentheses[LeetCode] + 回溯法
回溯法 百度百科:回溯法(探索与回溯法)是一种选优搜索法,按选优条件向前搜索,以达到目标.但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步又一次选择,这样的走不通就退回再走的技术为回溯法 ...
- 22. Generate Parentheses(ML)
22. Generate Parentheses . Generate Parentheses Given n pairs of parentheses, write a function to ge ...
- leetcode-algorithms-22 Generate Parentheses
leetcode-algorithms-22 Generate Parentheses Given n pairs of parentheses, write a function to genera ...
随机推荐
- [对对子队]会议记录5.19(Scrum Meeting6)
今天已完成的工作 吴昭邦 工作内容:搭建第9关 相关issue:搭建关卡7.8.9 相关签入:feat: 第9关能够通过 何瑞 工作内容:搭建第9关 相关issue:搭建关卡7.8 ...
- spring、spring boot中配置多数据源
在项目开发的过程中,有时我们有这样的需求,需要去调用别的系统中的数据,那么这个时候系统中就存在多个数据源了,那么我们如何来解决程序在运行的过程中到底是使用的那个数据源呢? 假设我们系统中存在2个数据源 ...
- pyqgis环境配置
配置pyqgis开发环境时,很多网上教程写的非常繁琐,这里仅仅找了一个最简单的配置方法,使用pycharm的IDE,安装QGIS软件后,在pycharm的ProjectInterpreter里面填写Q ...
- IPv6(诞生原因、数据报格式、与IPv4的不同、地址表现形式、基本地址类型、IPv6与IPv4的过渡策略)
文章转自:https://blog.csdn.net/weixin_43914604/article/details/105297642 学习课程:<2019王道考研计算机网络> 学习目的 ...
- hdfs基本操作命令
hdfs文件的相关操作主要使用hadoop fs.hadoop dfs.hdfs dfs 命令,以下对最常用的相关命令进行简要说明. hadoop fs -ls 显示当前目录结构,-ls -R 递归 ...
- vim 常用操作技巧
记录常用的vim操作技巧,基本满足90%的日常编辑使用. 文档操作 vim test.txt 打开当前目录下的test.txt文档,若不存在则创建该文件 :w 保存当前修改到文件 :w bak.txt ...
- threading python2 和python3
from __future__ import division from __future__ import print_function import threading balance = 0 d ...
- java线程同步以及对象锁和类锁解析(多线程synchronized关键字)
一.关于线程安全 1.是什么决定的线程安全问题? 线程安全问题基本是由全局变量及静态变量引起的. 若每个线程中对全局变量.静态变量只有读操作,而无写操作,一般来说,这个全局变量是线程安全的:若有多个线 ...
- Oracle 相关命令
http://www.mamicode.com/info-detail-2481866.html sql语句 system用户登陆 查看表空间和存放位置 select t1.name,t2.name ...
- 四种 AI 技术方案,教你拥有自己的 Avatar 形象
大火的 Avatar到底是什么 ? 随着元宇宙概念的大火,Avatar 这个词也开始越来越多出现在人们的视野.2009 年,一部由詹姆斯・卡梅隆执导 3D 科幻大片<阿凡达>让很多人认识了 ...