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. asp发布至IIS

    最近做的一个任务是使用asp写的,显示的是数据库的报表,但是将报表当前目录发布至IIS后,发现还是运行不了 Set conn = Server.CreateObject("ADODB.Con ...

  2. C# string类型遇到的两个问题

    最近在维护一位离职的同事写的WPF代码,偶然发现他使用C# string类型的两个问题,在这里记录一下. 1. 使用Trim函数移除字串中的空格.换行符等字符串. csRet.Trim(new cha ...

  3. BFC引发的关于position的思考

    BFC布局规则: 内部的Box会在垂直方向,一个接一个地放置. Box垂直方向的距离由margin决定.属于同一个BFC的两个相邻Box的margin会发生重叠 每个元素的margin box的左边, ...

  4. MVVM - 基础介绍

    MVVM模式:把页面UI和后台逻辑分开,这样做的好处是能使你的程序更容易测试,维护和改进.

  5. JAXB - Validate Document before It is Unmarshalled

    Validation A considerable part of the XML Schema language deals with facets, enabling the programmer ...

  6. linux下挂载iso镜像的方法

    新建目录/mnt/cdrom 执行命令 mount /dev/cdrom /mnt/cdrom [root@ocdp1 cdrom]# mount /dev/cdrom /mnt/cdrom moun ...

  7. 【转】代码高处走 从VC6到VC9移植代码问题说明

    首先可以直接用Visual Studio 2008的打开VC6的工作区文件和项目文件(dsw和dsp),并将其升级为VS2008的解决方案格式和项目格式(sln和vcproj),VC9的编译器相对于V ...

  8. 从CSDN转战于此

    平时喜欢写博客,开始是再CSDN,但CSDN博客功能实在不是很好.听说博客园的博客功能很不错,今天就来到此地.实际上很早就注册了博客园,但博客今天才申请的. 博客园,来了!

  9. iOS启动图片适配问题

    Portrait 是竖屏 top home button Landscape是横屏 left home button retina 要求640x1136pixels, 在右边的Image属性 Expe ...

  10. .NET小项目之MyKtv(歌曲播放功能实现)

    在KTV点歌系统中我们根据需求获取到歌手的歌曲信息,点击歌手的歌曲将其添加到一点歌曲列表中看似简单的一个操作其实涉及很多内容,这也是写这篇Blog的目的—分析歌曲播放的原理. 原理分析 我们应该清楚, ...