[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 ...
随机推荐
- uiatuomator命令启动apk,与查找多个相同控件
背景:在做项目时,发现使用uiatuomator中遇到了一些问题,现在把解决方法和思路分享出来 案列1:使用命令去启动要运用的apk包 在做自动化时,需要通过命令去启动APK的包,我使用的是sdk中自 ...
- js如何实现一定时间后去执行一个函数
js如何实现一定时间后去执行一个函数:在实际需要中可能需要规定在指定的时间之后再去执行一个函数以达成期望的目的,这也就是一个定时器效果,恰好在js中就已经给定了这样的一个函数setTimeout(), ...
- (原创)fedora 17安装KVM虚拟机
1.安装KVM yum groupinstall Virtualization 'Virtualization Client' 2.安装api支持 yum install libvirt servic ...
- oracle数据库不支持mysql中limit功能
oracle数据库不支持mysql中limit功能,但可以通过rownum来限制返回的结果集的行数,rownum并不是用户添加的字段,而是oracle系统自动添加的. (1)使查询结果最多返回前10行 ...
- JAXB - XML Schema Types, Binary Data
Data that has no "natural" representation with printable characters must, for inclusion in ...
- django 学习-1
1.首先是安装django 解压包然后 python setup.py install 安装成功 2.建立一个项目 django-admin.py startproject study 3. 再到 ...
- iOS创建UUID
- (NSString *)getUUID { CFUUIDRef uuidObj = CFUUIDCreate(nil); //create a new UUID NSString * uuidSt ...
- VirtualBox single usermode boot
VirtualBox single usermode boot 当系统因为某些原因无法通过图形界面登录VirtualBox内的系统时,可以通过Grub进入命令行模式/单一用户界面模式. 参考: 1.R ...
- vs 2010 Cannot find or open the PDB file
打开VS2010:工具-->选项-->>调试-->符号接下来就是选择Microsoft,然后确认 接着随便编译一个程序,过程会灰常的慢. 看到此目录下符号缓存了吗?C:\Us ...
- 在Windows下用MingW 4.5.2编译live555
1.下载live555(http://www.live555.com/liveMedia/public/),解压. 2.进入MingW Shell,输入cd: F:/Qt/live(假定解压到F:/Q ...