Implement an algorithm to print all valid ( properly opened and closed) combinations of n-pairs of parentheses.

思路:比如 n = 3, ((())) 就是一个valid combination. 这题和前一个 all permutations 有点类似,更 tricky 的地方在于存在大量的重复。

最好的办法是 使用一个 char 缓冲区,长度为2*n. 从左到右往这个缓冲区里填字符 '(' or ')',用一个index 指示 当前要填的位置。

那么什么时候填 ( , 什么时候填 ) 呢?规则如下:

假设 缓冲区已经填好一部分了,已经填好的部分里面有 x 个左括号,y 个右括号。

当 x <= n 时,说明左括号还没填完,可以填一个左括号,但是没说此时不能填右括号。

当 y < x <= n 时,此时可以填入右括号的同时保证 properly opened and closed.

其他的情况都是无效的。

  1. public void addParen(int index, int left, int right, char[] buffer,
  2. ArrayList<String> result){
  3.  
  4. int n = buffer.length / 2;
  5.  
  6. // ensure valid states
  7. if(left <= n && right <= n && left >= right){
  8.  
  9. if(left == n && right == n){
  10. result.add(new String(buffer));
  11. return;
  12. }
  13.  
  14. if(left < n){
  15. buffer[index] = '(';
  16. addParen(index+1, left+1, right, buffer, result);
  17. //don't return, continue instead
  18. }
  19.  
  20. if(right < left){
  21. buffer[index] = ')';
  22. addParen(index+1, left, right+1, buffer, result);
  23. }
  24.  
  25. }
  26. }
  27.  
  28. public ArrayList<String> generateParens(int n){
  29. char[] buffer = new char[n * 2];
  30. ArrayList<String> result = new ArrayList<String>();
  31. addParen(0, 0, 0, buffer, result);
  32. return result;
  33. }

[cc150] 括号问题的更多相关文章

  1. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

  2. 面试题目——《CC150》递归与动态规划

    面试题9.1:有个小孩正在上楼梯,楼梯有n个台阶,小孩一次可以上1阶.2阶或者3阶.实现一个方法,计算小孩有多少种上楼梯的方式. 思路:第4个数是前三个数之和 注意:能不能使用递归,能不能建立一个很大 ...

  3. javascript匹配各种括号书写是否正确

    今天在codewars上做了一道题,如下 看上去就是验证三种括号各种嵌套是否正确书写,本来一头雾水,一种括号很容易判断, 但是三种怎么判断! 本人只是个前端菜鸟,,不会什么高深的正则之类的. 于是,在 ...

  4. 明显调用的表达式前的括号必须具有(指针)函数类型 编译器错误 C2064

    看到“明显调用的表达式前的括号必须具有(指针)函数类型”这句时我才发现我的语文水平有多烂,怎么看都看不懂,折腾了半天才知道是哪里出了问题. 举个简单的例子 class CTest { void (CT ...

  5. [LeetCode] Remove Invalid Parentheses 移除非法括号

    Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...

  6. [LeetCode] Different Ways to Add Parentheses 添加括号的不同方式

    Given a string of numbers and operators, return all possible results from computing all the differen ...

  7. [LeetCode] Longest Valid Parentheses 最长有效括号

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  8. [LeetCode] Generate Parentheses 生成括号

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

  9. [LeetCode] Valid Parentheses 验证括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

随机推荐

  1. Chrome&FF&Opera&下DIV不设置高度显示背景颜色和边框的办法

    今天在排版的时候,外层的div不写高度的话背景颜色和边框没法办法显示,但是在IE下面就可以,这个有三个解决办法. 第一: 直接给最外层的div设置高度(不推荐). 第二: 在内部每个div后添加一个清 ...

  2. C#导入导出Excel表的数据

    一:C#导入导出EXCEL文件的类 代码如下: 首先将Microsoft Excel 14.0 Object Library 引用导入 using System; using System.Data; ...

  3. Linux系统下安装ISO文件

    1.在/mnt目录下,创建相应的iso文件夹,例如cd1,cd2. 2.然后,用命令:mount -o loop /home/kinglu/Matlab/matlab1.iso(iso文件路径名) / ...

  4. 《Pro Git》学习笔记

    1.Git远程模型示意图 Remote:远程仓库 Repository:本地仓库 Index:暂存区 workspace:当前工作区 2.取得Git仓库 2.1 初始化新仓库     git init ...

  5. web框架--来自维基百科

  6. 【原创】java 流星划过天空

    import java.awt.Color; import java.awt.Graphics; import java.awt.image.BufferedImage; import javax.s ...

  7. linux下设置mysql数据库字符集utf8

    mysql中文乱码解决方法:将mysql数据库编码统一utf8 查看数据库编码: show variables like 'character%'; 编辑/etc/my.cnf [mysql] def ...

  8. arcgis中的 style和serverstyle

    转自网络,原出处不明. .style文件是用ArcMap工具导出的符号库文件 .serverStyle文件是ArcEnging可用的符号库文件 需要转换才能使用. 第一:把.style文件拷贝到D:主 ...

  9. Jquery中Ajax异步请求中的async参数的作用

    之前不知道这个参数的作用,上网找了前辈的博客,在此收录到自己的博客,希望能帮到更多的朋友: test.html <a href="javascript:void(0)" on ...

  10. sea.js总结

    SeaJS是一个遵循CommonJS规范的JavaScript模块加载框架. 参考以下网址进行详细学习: https://segmentfault.com/a/1190000000357191?pag ...