原文链接: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,边搜语法边照着敲的。肯定用的很蹩脚。见谅。

  1. class Solution {
  2. public:
  3. vector<string> generateParenthesis(int n) {
  4. vector<int>left;  //第i个字符串左括号的个数
  5. vector<int>right;  //第i个字符串右括号个数
  6. vector<string> ans;
  7. int i,j,k;
  8. string tmp("(");
  9.  
  10. ans.push_back(tmp);  //第一个字符串就是"("
  11. left.push_back();  //第一个字符串的左括号有一个
  12. right.push_back();  //右括号0。
  13.  
  14. for(i = ; i<*n; i++)  //开始添加第i位。
  15. {
  16. int len = ans.size();
  17. for(j = ;j < len; j++)  //遍历每个字符串
  18. {
  19. if(left[j] == right[j])
  20. {
  21. ans[j].append(,'(');
  22. left[j] ++;
  23. }
  24. else if(left[j]>right[j] && left[j] < n)
  25. {
  26. string tmp(ans[j]);
  27. tmp.append(,'(');
  28. ans.push_back(tmp);
  29. left.push_back(left[j]+);
  30. right.push_back(right[j]);
  31.  
  32. ans[j].append(,')');
  33. right[j]++;
  34. }
  35. else if(left[j] == n)
  36. {
  37. ans[j].append(,')');
  38. right[j]++;
  39. }
  40. }
  41. }
  42. return ans;
  43. }
  44. };

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. mysql查询优化之四:优化特定类型的查询

    本文将介绍如何优化特定类型的查询. 1.优化count()查询count()聚合函数,以及如何优化使用了该函数的查询,很可能是mysql中最容易被误解的前10个话题之一 count() 是一个特殊的函 ...

  2. 廖雪峰Java5集合-4Set-1使用Set

    集合 Set用于存储不重复的元素集合: boolean add(E e) boolean remove(Object o) boolean contains(Object o) int size() ...

  3. Oracle group by

    group by 简单点理解就是根据什么分组 为此 group by job   根据job进行分组 举例:在Oracle当中scott/oracle 下有emp表 进行如下操作  体会order b ...

  4. 阿里云线上ROS静态路由转发,有大坑。

    原因见上去,阿里云不支持VPC中转流量,VPC1和VPC2都在国内,VPC3在香港,如果按阿里云的做法,必须付费2次国际隧道的钱,才可以实现三个VPC互通.明显很浪费钱. 所以我们只能在三个VPC,各 ...

  5. 在 Element-UI 的 Table 组件上添加列拖拽效果

    Element-UI 的 Table组件很强大,但是我们的需求更强大... 简单粗暴的来一发效果图: 一.数据驱动 传统的拖动效果,都是基于通过 mousedown.mousemove.mouseup ...

  6. windows server 2008 R2 安装

    微软服务器操作系统大致有: server 2000(简称2K),还有server 2003(2K3),server 2008(2K8),server 2000和2003是基于NT内核的,而2008是基 ...

  7. [UE4]关于分支Sequence和条件分支的组合用法

    当需要不管条件语句是否成立的后面都需要执行的语句,可以使用“Sequence”来分支,达到简化蓝图连线的目的.如下图所示:

  8. 字符串格式化format使用

    顺序传参 '{}....{}'.format(value1, value2) 索引传参 '{0}....{1}'.format(value1, value2) 关键字传参 '{k1}....{k2}' ...

  9. 修改IP和DNS的dos命令

    修改IP,掩码,网关命令: netsh interface ip set address "本地连接" static 192.168.3.188 255.255.255.0 192 ...

  10. spark使用hadoop native库

    默认情况下,hadoop官方发布的二进制包是不包含native库的,native库是用C++实现的,用于进行一些CPU密集型计算,如压缩.比如apache kylin在进行预计算时为了减少预计算的数据 ...