[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 ...
随机推荐
- [转]ASP.NET MVC中你必须知道的13个扩展点
本文转自:http://www.cnblogs.com/ejiyuan/archive/2010/03/09/1681442.html ScottGu在其最新的博文中推荐了Simone Chiaret ...
- 将Oracle表导入到Access方法,已经安装了Access但Win7提示 找不到access driver
已经安装了Access但Win7提示 找不到access driver: 1.打开 "C:\Windows\SysWOW64\odbcad32.exe" 2.管理员cmd运行 命令 ...
- 关于struts2的上传和下载
1. 1文件上传技术: JSPSmartUpload:应用在Model1年代.(嵌入到JSP) FileUpload:应用在Model2年代. Servlet3.o:完成文件上传. Struts2框架 ...
- Executor 和Executors
Java里面线程池的顶级接口是Executor,但是严格意义上讲Executor并不是一个线程池,而只是一个执行线程的工具.真正的线程池接口是ExecutorService. 下面这张图完整描述了线程 ...
- layer弹出层不居中解决方案,layer提示不屏幕居中解决方法,layer弹窗不居中解决方案
layer弹出层不居中解决方案,layer提示不屏幕居中解决方法,layer弹窗不居中解决方案 >>>>>>>>>>>>> ...
- c# 委托与异步调用
背景:在winform UI中,有时需要对控件进行比较频繁的刷新,如进度条.picturebox显示视频等.如果在主线程进行这些刷新操作,操作还未完成就将执行下一次刷新,程序将发生错误:如果只是创建另 ...
- 猪满满 购物APP
猪满满是专注“省钱,赚钱”的购物App,使用自定义tabar分为四大类,分别是首页,超返,发现,我的. 首页:使用UItableview,自定义cell展示商品. 超返:自定义Button分为综合,返 ...
- ASP清除字串中的重复字符
<% Function Test(str) dim intLen,i,strTemp,aryTest intLen = Len(str) strTemp = "" aryTe ...
- ASP长文章分页的两个方法,函数
'按标识手动分页 function manualPage(str) pages=request.QueryString("page") contentstr=split(str,& ...
- THP Transparent HugePages关闭
ambari 安装Hortonworks HDP 时在检测host异常 The following hosts have Transparent Huge Pages (THP) enabled.TH ...