原文链接:https://leetcode.com/problems/generate-parentheses

给出数字n,求n对括号组成的合法字符串。

刚做出来,敲完代码,修改了一次,然后提交,accepted ~ 还是小有成就感的。。菜鸟...

先记录下自己的笨方法。再研究更高妙的方法。

刚看到题也是一头雾水。没法深入思考,感觉就是不断的罗列,被题目吓到了。

仔细观察,合法字符串的第一个字母肯定是( ,最后一个肯定是)。

对于合法字符串的每一位,必定有 左括号的个数 >= 右括号的个数。每一位都必须满足。

想法就是,从第一位开始向最后一位扩充,直到填满2*n位。

当填第i位的时候,考察前面左括号的个数left,和右括号的个数right。有下面三种情况:

1、left == right 左右括号相等了,这时候只能填左括号。

2、left == n , right < n  左括号已经有n个了,满了,只能填右括号了。

3、left < n && left > right  左括号还没满,且比右括号多。这时候,可以填两种。

本来想递推算,但是后来习惯性改成了循环。

用C太麻烦了!

C++的STL,边搜语法边照着敲的。肯定用的很蹩脚。见谅。

class Solution {
public:
vector<string> generateParenthesis(int n) {
vector<int>left;  //第i个字符串左括号的个数
vector<int>right;  //第i个字符串右括号个数
vector<string> ans;
int i,j,k;
string tmp("("); ans.push_back(tmp);  //第一个字符串就是"("
left.push_back();  //第一个字符串的左括号有一个
right.push_back();  //右括号0。 for(i = ; i<*n; i++)  //开始添加第i位。
{
int len = ans.size();
for(j = ;j < len; j++)  //遍历每个字符串
{
if(left[j] == right[j])
{
ans[j].append(,'(');
left[j] ++;
}
else if(left[j]>right[j] && left[j] < n)
{
string tmp(ans[j]);
tmp.append(,'(');
ans.push_back(tmp);
left.push_back(left[j]+);
right.push_back(right[j]); ans[j].append(,')');
right[j]++;
}
else if(left[j] == n)
{
ans[j].append(,')');
right[j]++;
}
}
}
return ans;
}
};

leetcode题解 Generate Parentheses的更多相关文章

  1. [LeetCode 题解]: Generate Parentheses

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

  2. 【题解】【排列组合】【回溯】【Leetcode】Generate Parentheses

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

  3. LeetCode 022 Generate Parentheses

    题目描述:Generate Parentheses Given n pairs of parentheses, write a function to generate all combination ...

  4. leetcode@ [22]Generate Parentheses (递归 + 卡特兰数)

    https://leetcode.com/problems/generate-parentheses/ Given n pairs of parentheses, write a function t ...

  5. leetcode之 Generate Parentheses

    题目:http://oj.leetcode.com/problems/generate-parentheses/ 描述:给定一个非负整数n,生成n对括号的所有合法排列. 解答: 该问题解的个数就是卡特 ...

  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

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

  8. 【leetcode】Generate Parentheses

    题目简述: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...

  9. 【leetcode】 Generate Parentheses (middle)☆

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

随机推荐

  1. Java-Runoob-高级教程-实例-数组:16. Java 实例 - 数组并集

    ylbtech-Java-Runoob-高级教程-实例-数组:16. Java 实例 - 数组并集 1.返回顶部 1. Java 实例 - 数组并集  Java 实例 以下实例演示了如何使用 unio ...

  2. JSON 简介

    ylbtech-JSON: JSON 简介 JSON:JavaScript Object Notation(JavaScript 对象表示法) JSON是存储和交换文本信息的语法,类似 XML. JS ...

  3. 学习笔记之Machine Learning by Andrew Ng | Stanford University | Coursera

    Machine Learning by Andrew Ng | Stanford University | Coursera https://www.coursera.org/learn/machin ...

  4. 廖雪峰Java2面向对象编程-1面向对象-1面向对象基础

    1.对象的概念 面向对象编程:Object-Oriented Programming 对现实世界建立计算机模型的一种编程方法. 现实世界 计算机模型 Java代码 人 类/class class Pe ...

  5. redis-5.0.3集群搭建

    首先部署redis-5.0.3,请参考我的另一篇文章 https://www.cnblogs.com/djlsunshine/p/10592174.html 启动redis服务 # redis-ser ...

  6. Redis-Migrate-Tool 使用详解

    注意:目前不支持4.0.X及以上的redis使用 Redis 集群迁移工具,基于redis复制,快速,稳定. github链接:https://github.com/vipshop/redis-mig ...

  7. Oracle SQL:select各类查询语句总结

    SQL查询语句总结 数据查询语言:即DML(Data Mannipulation Language)数据操纵语言,用于查询.操纵数据表资料行 本手册所有示例引用表均以TIPTOP GP  ERP数据库 ...

  8. Javascript-关于for in和forEach

    JS-for in:用来遍历对象 //遍历对象 for in var opts={name:'xiaofei',age:'28岁',job:'web前端工程师'} for (var k in opts ...

  9. WordPress版微信小程序1.5版本发布

    这个周末对WordPress版微信小程序进行了小版本的升级,第一个版本做得比较粗糙,性能上也有些差.本次升级主要调整和优化功能包括: 1.在主页面,加入浮动按钮,用来打开侧滑导航菜单. 2.增加侧滑导 ...

  10. 阿里云发送短信验证码php_SDK

    1.登录阿里云账号下载——aliyun-dysms-php-sdk(我使用的php版本) 下载地址:https://help.aliyun.com/document_detail/55359.html ...