一、题目说明

这个题目是22. Generate Parentheses,简单来说,输入一个数字n,输出n对匹配的小括号。

简单考虑了一下,n=0,输出"";n=1,输出“()”;n=2,输出“()()”,"(())"...

二、我的解法

我考虑了一下,基本上2种思路,其1是暴力破解法,就是列出所有可能情况,然后判断哪些是匹配的。

汗颜的是,我没做出来。找到的答案:

#include<iostream>
#include<vector>
#include<string>
using namespace std; class Solution {
public:
//Brute Force
vector<string> generateParenthesis(int n) {
int num = 2*n;
string cur(num,'0');
vector<string> ans;
//1左移num位得到2的2*n次方
for (int v = (1<<num)-1; v > 0; v--) {
//生成 所有的0、1序列
// for (int i = 0; i < num; i++) {
// if(v & 1<<i) cur[i] = '(';
// else cur[i]=')';
// }
// ans.push_back(cur); int cnt = 0;
for (int i = 0; i < num; i++) {
if (v&1<<i) { cur[i]='('; cnt++; }
else { cur[i]=')'; cnt--; }
if (cnt < 0 || cnt > n) break;
}
if (cnt == 0) ans.push_back(cur);
}
return ans;
}; void test(int n){
string cur(n,'0');
for(int v=(1<<n)-1;v>=0;v--){
for(int j=0;j<n;j++){
if(v & 1<<j) cur[j] = '1';
else cur[j] = '0';
}
cout<<cur<<"\n";
}
}
}; int main(){
Solution s;
// s.test(2);
vector<string> r = s.generateParenthesis(1);
cout<<r.size()<<endl;
for(int i=0;i<r.size();i++){
cout<<r[i]<<endl;
}
return 0;
}

其2是回溯法,汗颜的是,我也没做出来,网上找到的答案:

#include<iostream>
#include<vector>
using namespace std; class Solution {
public: //Backtracking
vector<string> generateParenthesis(int n)
{
if(!n) return vector<string>(1, "");
if(n == 1) return vector<string>(1, "()"); vector<string> result; for(int i = n-1; i >=0; i--)
{
vector<string> inner = generateParenthesis(i);
vector<string> outer = generateParenthesis(n-i-1); for(int i=0; i < inner.size(); i++)
for(int j=0; j < outer.size(); j++)
result.push_back( "(" + inner[i] + ")" + outer[j] );
} return result;
}
};
int main(){
Solution s;
vector<string> r = s.generateParenthesis(3);
for(int i=0;i<r.size();i++){
cout<<r[i]<<endl;
}
return 0;
}

三、优化措施

另外,学过离散数学的,还有一种方法就是“闭包”。似曾相识,直接上答案:

class Solution {
public: //Imporved Closure Number
vector<string> generateParenthesis(int n) {
map<int, vector<string>> map;
map[0].push_back("");
for(int i = 1; i <= n; i++)
for(int c = 0; c < i; c++)
for(string left: map[c])
for(string right: map[i - 1 - c])
map[i].push_back("(" + left + ")" + right);
return map[n];
}
};

刷题22. Generate Parentheses的更多相关文章

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

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

  2. 22. Generate Parentheses(ML)

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

  3. 【LeetCode】22. Generate Parentheses (2 solutions)

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

  4. 22. Generate Parentheses (recursion algorithm)

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

  5. 22. Generate Parentheses产生所有匹配括号的方案

    [抄题]: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...

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

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

  7. 【LeetCode】22. Generate Parentheses (I thought I know Python...)

    I thought I know Python... Actually , I know nothing... 这个题真想让人背下来啊,每一句都很帅!!! Given n pairs of paren ...

  8. 【一天一道LeetCode】#22. Generate Parentheses

    一天一道LeetCode (一)题目 Given n pairs of parentheses, write a function to generate all combinations of we ...

  9. LeetCode OJ 22. Generate Parentheses

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

随机推荐

  1. rabbit MQ 消息队列

    为什么会需要消息队列(MQ)? 一.消息队列概述消息队列中间件是分布式系统中重要的组件,主要解决应用解耦,异步消息,流量削锋等问题,实现高性能,高可用,可伸缩和最终一致性架构.目前使用较多的消息队列有 ...

  2. light oj1028 - Trailing Zeroes (I)

    1028 - Trailing Zeroes (I)   We know what a base of a number is and what the properties are. For exa ...

  3. oracle11g R2数据库的迁移(同windows系统迁移)使用RMAN

    实验环境:windows 2008 R2 & windows 2008 R2 Oracle版本:11.2.0.1.0 源数据库端: 为保证在恢复之后的数据库中得到一致的数据,应禁止用户对数据的 ...

  4. textarea 标签

    textarea 标签 -- 代表HTML表单多行输入域 textarea标签是成对出现的,以<textarea>开始,以</textarea>结束 属性: Common -- ...

  5. 惊讶!缓存刚Put再Get居然获取不到?

    最近一直在老家远程办公,微信突然响了下,有同事说遇到了一个奇怪的问题,让我帮忙看下. 现象就是标题所说的缓存获取不到的问题,我一听感觉这个问题挺有意思的,决定一探究竟. 下面给出部分代码还原下案发现场 ...

  6. 优雅的C#

    @符号:字符串中的转义符不转义,可支持字符串换行,例如:string test = "hello\\",正常输出hello\,改成string test = @"hell ...

  7. JavaScript-装饰器模式

    装饰器模式 为对象添加新功能 不改变其原有的结构和功能 传统 UML 类图 javascript 中的装饰器 装饰类 @testDec clss Demo { } function testDec(t ...

  8. xPath和html基础扫盲

    xPath:一种HTML和XML的查询语言,他能在XML和HTML的树状结构中寻找节点 安装xPath: pip方法: pip install  lxml win+R:cmd 打开命令控制台:   此 ...

  9. vue移动端下拉刷新、上拉加载

    由于自身的项目比较简单,只有几个H5页面,用来嵌入app中,所有没有引入移动端的UI框架,但是介于能让用户在浏览H5页面时有下拉刷新和上拉加载,有更好的用户体验,自己写组件实现. 1.下拉刷新Drop ...

  10. Linux下使用Tomcat

    切换到root账户. tomcat依赖jdk,先安装jdk,注意tomcat对jdk的版本有要求,要看一下tomcat.jdk的版本是否对应. 1.下载tomcat7 不使用软件源,自己下载安装,这样 ...