一、题目说明

这个题目是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. Linux设备中的UUID

    UUID简介 UUID为系统中的存储设备提供唯一的标识字符串,不管这个设备是什么类型的.如果你在系统中启动的时候,使用盘符挂载时,可能找不到设备而加载失败,而使用UUID挂载时,则不会有这样的问题.( ...

  2. 软件bug描述(android)

    1.bug主题:主要操作+bug主题 主题要简单明了,即开发一看主题就知道该问题. 2.描述: 作用:便于开发重现和定位缺陷的 2.1前置条件 2.2操作步骤 2.3预期结果 2.4实际结果 2.5备 ...

  3. JWT实现token-based会话管理(转)

    JWT实现token-based会话管理   阅读目录 认识JWT demo要点说明 小结 上文<3种web会话管理的方式>介绍了3种会话管理的方式,其中token-based的方式有必要 ...

  4. solr常用操作及集成分词器或cdh集群部署说明

    首先,如果是从http://lucene.apache.org/solr/下载的solr,基本都是自带集成的jetty服务,不需要单独搭建tomcat环境,但是要注意jdk版本,直接解压通过cmd命令 ...

  5. 第一篇 Springboot + Web MVC + MyBatis + 简单UI + Thymeleaf实现

    源码链接:https://pan.baidu.com/s/1-LtF56dnCM277v5lILRM7g 提取码:c374 第二篇 Springboot mybatis generate根据数据库表自 ...

  6. Executor 任务执行器

    Executor: 是一个接口 用于执行提交的任务 解耦任务提交和执行(线程的创建及调度) Executor的实现可以根据实际需求延展不同的逻辑:1. 对于提交的任务同步或者异步执行,如下同步执行: ...

  7. python基础入门之四 —— 列表

    1.格式 [数据1,数据2,数据3,...] 列表可以一次性存多个数据,可以为不同的数据类型 2.下标 从0开始循序向下分配 3.常用函数 查找 index():返回指定数据所在位置下标,不存在就报错 ...

  8. 分布式系统的CAP定理

    CAP定理: 在一个分布式系统中,Consistency(数据一致性). Availability(服务可用性).Partition tolerance(分区容错性),三者不可兼得. 一致性(Cons ...

  9. 《自拍教程22》wget_文件下载工具

    wget用途介绍 日常测试过程中,我们可以用wget命令,来下载一些资源文件. wget是一个很好文件下载命令, Linux操作系统下,自带wget命令. Windows操作系统下,需要自己去下载并配 ...

  10. 【终端命令】组管理 和 Ubuntu中的"sudo"命令

    一.超级用户root 1.超级用户和标准用户 Linux系统中的root账户通常 用于系统的维护和管理,对操作系统的 所有资源 具有所有访问权限. 在大多数版本的Linux系统中,都 不推荐 直接使用 ...