[cc150] 括号问题
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] 括号问题的更多相关文章
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- 面试题目——《CC150》递归与动态规划
面试题9.1:有个小孩正在上楼梯,楼梯有n个台阶,小孩一次可以上1阶.2阶或者3阶.实现一个方法,计算小孩有多少种上楼梯的方式. 思路:第4个数是前三个数之和 注意:能不能使用递归,能不能建立一个很大 ...
- javascript匹配各种括号书写是否正确
今天在codewars上做了一道题,如下 看上去就是验证三种括号各种嵌套是否正确书写,本来一头雾水,一种括号很容易判断, 但是三种怎么判断! 本人只是个前端菜鸟,,不会什么高深的正则之类的. 于是,在 ...
- 明显调用的表达式前的括号必须具有(指针)函数类型 编译器错误 C2064
看到“明显调用的表达式前的括号必须具有(指针)函数类型”这句时我才发现我的语文水平有多烂,怎么看都看不懂,折腾了半天才知道是哪里出了问题. 举个简单的例子 class CTest { void (CT ...
- [LeetCode] Remove Invalid Parentheses 移除非法括号
Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...
- [LeetCode] Different Ways to Add Parentheses 添加括号的不同方式
Given a string of numbers and operators, return all possible results from computing all the differen ...
- [LeetCode] Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [LeetCode] Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [LeetCode] Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
随机推荐
- 关于Git的工作区域和对应的文件状态.
Git是一个免费的.分布式的版本控制工具,或是一个强调了速度快的源代码管理工具.每一个Git的工作目录都是一个完全独立的代码库,并拥有完整的历史记录和版本追踪能力,不依赖于网络和中心服务器. Git ...
- Linux编程简介
Linux编程可以分为Shell(如BASH.TCSH.GAWK.Perl.Tcl和Tk等)编程和高级语言(C语言,C++语言,java语言等)编程,Linux程序需要首先转化为低级机器语言即所谓的二 ...
- 为ubuntu只带的network-manager添加latp/ipsec VPN
sudo apt-add-repository ppa:seriy-pr/network-manager-l2tp sudo apt-get update sudo apt-get install n ...
- Executor 和Executors
Java里面线程池的顶级接口是Executor,但是严格意义上讲Executor并不是一个线程池,而只是一个执行线程的工具.真正的线程池接口是ExecutorService. 下面这张图完整描述了线程 ...
- 最佳vim技巧
最佳vim技巧----------------------------------------# 信息来源----------------------------------------www.vim ...
- Ext.Net学习笔记17:Ext.Net GridPanel Selection
Ext.Net学习笔记17:Ext.Net GridPanel Selection 接下来是Ext.Net的GridPanel的另外一个功能:选择. 我们在GridPanel最开始的用法中已经见识过如 ...
- C# Generic(转载)
型(generic)是C#语言2.0和通用语言运行时(CLR)的一个新特性.泛型为.NET框架引入了类型参数(type parameters)的概念.类型参数使得设计类和方法时,不必确定一个或多个具体 ...
- Java JDK1.5、1.6、1.7新特性整理(转)
原文链接:http://www.cnblogs.com/tony-yang-flutter/p/3503935.html 一.Java JDK1.5的新特性 1.泛型: List<String& ...
- 九度OJ 1501 最大连续子序列乘积 -- 动态规划
题目地址:http://ac.jobdu.com/problem.php?pid=1501 题目描述: 给定一个浮点数序列(可能有正数.0和负数),求出一个最大的连续子序列乘积. 输入: 输入可能包含 ...
- OpenJudge 2810(1543) 完美立方 / Poj 1543 Perfect Cubes
1.链接地址: http://bailian.openjudge.cn/practice/2810/ http://bailian.openjudge.cn/practice/1543/ http:/ ...