原文链接: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. [转][CentOS]修改IP后立即生效

    来自:http://bbs.51cto.com/thread-789908-1.html Linux系统里修改IP地址后该如何使之即刻生效,有两种方法可以解决: (1) sudo ifdown eth ...

  2. 面向对象php 接口 抽象类

    1.定义类和实例化对象: 使用关键字class定义类,使用new实例化对象: 2.类成员的添加和访问: 类成员:有属性,方法,常量(常量名不带$符): 访问属性的时候,变量名不带$符 添加属性需要使用 ...

  3. 修改ECSHOP的小数点保留位数

    客户站点http://carfa.hk79.2ifree.com 原来的程序直接取整了,现在做下面修改. 首先打开文件 /carfa/web/includes/lib_common.php 第一步:在 ...

  4. HDOJ 2005 第几天?

    #include<iostream> using namespace std; ] = { ,,,,,,,,,,, }; ] = { ,,,,,,,,,,, }; bool isRYear ...

  5. [UE4]通过使用Set TimerByFunctionName来实现反射机制

  6. 0002 - Spring MVC 拦截器源码简析:拦截器加载与执行

    1.概述 Spring MVC中的拦截器(Interceptor)类似于Servlet中的过滤器(Filter),它主要用于拦截用户请求并作相应的处理.例如通过拦截器可以进行权限验证.记录请求信息的日 ...

  7. JIT和AOT编译详解

    JIT和AOT编译介绍 JIT - Just-In-Time             实时编译,即时编译 通常所说的JIT的优势是Profile-Based Optimization,也就是边跑边优化 ...

  8. spark streaming检查点使用

    import org.apache.spark._ import org.apache.spark.streaming._ /** * Created by code-pc on 16/3/14. * ...

  9. visual studio 2017调试时闪退。

    解决方案: 在工程上右键--->属性--->配置属性--->连接器--->系统--->子系统(在窗口右边)--->下拉框选择控制台(/SUBSYSTEM:CONSO ...

  10. python第三方库,你要的这里都有

    Python的第三方库多的超出我的想象. python 第三方模块 转 https://github.com/masterpy/zwpy_lst   Chardet,字符编码探测器,可以自动检测文本. ...