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.

其他的情况都是无效的。

public void addParen(int index, int left, int right, char[] buffer,
ArrayList<String> result){ int n = buffer.length / 2; // ensure valid states
if(left <= n && right <= n && left >= right){ if(left == n && right == n){
result.add(new String(buffer));
return;
} if(left < n){
buffer[index] = '(';
addParen(index+1, left+1, right, buffer, result);
//don't return, continue instead
} if(right < left){
buffer[index] = ')';
addParen(index+1, left, right+1, buffer, result);
} }
} public ArrayList<String> generateParens(int n){
char[] buffer = new char[n * 2];
ArrayList<String> result = new ArrayList<String>();
addParen(0, 0, 0, buffer, result);
return result;
}

[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. gcc的使用

    为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/ShiJiaqi. http://www.cnblogs.com/shijiaqi1066/p/6065410. ...

  2. css3过度和动画

    一.过度(transition) transition:[transition-property] || [transition-duration] || [transition-timing-fun ...

  3. FileReader 的了解

    FileReader的解释 异步的读取存储在用户计算机上的文件 创建一个FileReader 对象 var reader = new FileReader(); FileReader的方法和事件 参数 ...

  4. 快速开启Windows 的各种任务及 bat(ch)脚本

    MSC It is the Microsoft Management Console Snap-in Control File, like services.msc, devmgmt.msc (Dev ...

  5. [leetcode] 398. Random Pick Index

    我是链接 看到这道题,想到做的几道什么洗牌的题,感觉自己不是很熟,但也就是rand()函数的调用,刚开始用map<int, vector<int >>来做,tle,后来就想着直 ...

  6. 改善EF代码的方法(下)

    本节,我们将介绍一些改善EF代码的方法,包括编译查询.存储模型视图以及冲突处理等内容. > CompiledQuery 提供对查询的编译和缓存以供重新使用.当相同的查询需要执行很多遍的时候,那么 ...

  7. windows phone 生产二维码和解码本地二维码图片

    前面模仿着写了一个手机扫描二维码和条形码的例子,zxing(下载)的Silverlight库实现的,当时还纳闷有windows phone的库为什么不用,其实都是一样的,,,要改的就是获取摄像头获取的 ...

  8. IOPS和Throughput

    IOPS和Throughput吞吐量两个参数是衡量存储性能的主要指标.IOPS表示存储每秒传输IO的数量,Throughput吞吐量则表示每秒数据的传输总量.两者在不同的情况下都能表示存储的性能状况, ...

  9. JS到PHP使用RSA算法进行加密通讯

    我们平时做用户登录表单提交,用户名密码都是明文直接POST到后端,这样很容易被别人从监听到. 在js上做rsa,感觉jsencrypt这个是封装的比较好的,但用起来还是遇到了些坑,所以踩进代码里填填坑 ...

  10. Date、String、Calendar类型之间的转化

    原文出处:http://fjfj910.iteye.com/blog/1202219 1.Calendar 转化 String  //获取当前时间的具体情况,如年,月,日,week,date,分,秒等 ...