Careercup - Google面试题 - 6253551042953216
2014-05-06 01:49
原题:
Modify the following code to add a row number for each line is printed public class Test {
public static void main(String [] args){
printParenthesis(3);
}
public static void printParenthesis(int n){
char buffer[] = new char[n*2];
printP(buffer,0,n,0,0);
}
public static void printP(char buffer[], int index, int n, int open, int close){
if(close == n){
System.out.println(new String(buffer));
}else{
if(open > close){
buffer[index] = ']';
printP(buffer, index+1, n, open, close+1);
}
if(open < n ){
buffer[index] = '[';
printP(buffer,index+1,n,open+1,close);
}
}
}
} Expected Output: 1.[][][]
2.[][[]]
3.[[]][]
4.[[][]]
5.[[[]]] What changes needs to be done to accomplish the output expected?
题目:给下面的代码加上一些修改,使得输出的结果能带有序号,如示例中的格式。
解法:代码可能还不太明显,但从结果一看就知道是输出N对括号匹配的所有组合,并且卡塔兰数H(3) = 5,也符合条件。只要加上一个全局的counter,并且在输出语句附近给counter加1,就可以带序号输出了。
代码:
// http://www.careercup.com/question?id=6253551042953216
public class Test {
static int res_count = 0; public static void main(String [] args) {
printParenthesis(3);
} public static void printParenthesis(int n) {
char buffer[] = new char[n * 2];
res_count = 0;
printP(buffer, 0, n, 0, 0);
} public static void printP(char buffer[], int index, int n, int open, int close) {
if(close == n) {
System.out.print((++res_count) + ".");
System.out.println(new String(buffer));
} else {
if (open > close) {
buffer[index] = ']';
printP(buffer, index+1, n, open, close + 1);
}
if (open < n) {
buffer[index] = '[';
printP(buffer, index + 1, n, open + 1, close);
}
}
}
}
Careercup - Google面试题 - 6253551042953216的更多相关文章
- Careercup - Google面试题 - 5732809947742208
2014-05-03 22:10 题目链接 原题: Given a dictionary, and a list of letters ( or consider as a string), find ...
- Careercup - Google面试题 - 5085331422445568
2014-05-08 23:45 题目链接 原题: How would you use Dijkstra's algorithm to solve travel salesman problem, w ...
- Careercup - Google面试题 - 4847954317803520
2014-05-08 21:33 题目链接 原题: largest number that an int variable can fit given a memory of certain size ...
- Careercup - Google面试题 - 6332750214725632
2014-05-06 10:18 题目链接 原题: Given a ,) (,) (,), (,) should be returned. Some suggest to use Interval T ...
- Careercup - Google面试题 - 5634470967246848
2014-05-06 07:11 题目链接 原题: Find a shortest path ,) to (N,N), assume is destination, use memorization ...
- Careercup - Google面试题 - 5680330589601792
2014-05-08 23:18 题目链接 原题: If you have data coming in rapid succession what is the best way of dealin ...
- Careercup - Google面试题 - 5424071030341632
2014-05-08 22:55 题目链接 原题: Given a list of strings. Produce a list of the longest common suffixes. If ...
- Careercup - Google面试题 - 5377673471721472
2014-05-08 22:42 题目链接 原题: How would you split a search query across multiple machines? 题目:如何把一个搜索que ...
- Careercup - Google面试题 - 6331648220069888
2014-05-08 22:27 题目链接 原题: What's the tracking algorithm of nearest location to some friends that are ...
随机推荐
- C#处理Excel
C#处理Excel C#处理Excel 前言 OleDb 具体操作 NPOI 具体操作 Excel C# NPOI OleDb 前言 最近需要对Excel进行加密解密操作,本身是一个简单的事情,通过 ...
- 刚开始学IOS遇到的类和方法
框架:Core FoundationCFGetRetainCount. 类:NSRunLoop.NSAutoreleasePool.NSStringFormClass.UIApplicationMai ...
- Hive中典型的表内数据除重写法
insert overwrite table store select t.p_key,t.sort_word from ( select p_key, sort_word , row_number( ...
- redis 入门
1.命令行工具 在windows上巧命令行指令,实在是令人痛苦,本人实在是受不了windows下cmd的笨,powershell的蠢,只能换一个了. 介绍一款cmd工具cmder(github上开源) ...
- linux中文显示乱码的解决办法
linux中文显示乱码的解决办法 linux中文显示乱码是一件让人很头疼的事情. linux中文显示乱码的解决办法:[root@kk]#vi /etc/sysconfig/i18n将文件中的内容修改为 ...
- Nginx安装第二步手动下载依赖包
nginx可以使用各平台的默认包来安装,本文是介绍使用源码编译安装,包括具体的编译参数信息. 正式开始前,编译环境gcc g++ 开发库之类的需要提前装好,这里默认你已经装好. ububtu平台编译环 ...
- 09-排序2 Insert or Merge
要点就是把排序每一步,判断一下是否和第二组数据相同,若相同则输出排序方法和下一次序列. According to Wikipedia: Insertion sort iterates, consumi ...
- 19.python的编码问题
在正式说明之前,先给大家一个参考资料:戳这里 文章的内容参考了这篇资料,并加以总结,为了避免我总结的不够完善,或者说出现什么错误的地方,有疑问的地方大家可以看看上面那篇文章. 以下说明是针对于pyth ...
- Sublime Text 2编译python时出错
[Error 2] The system cannot find the file specified [Finished] 解决方法: 1.环境变量path添加: C:\Python32\Too ...
- WPF工作笔记:本地化支持、主进程通知、两种最常用异步编程方式
1.本地化支持 (1)重写控件默认的依赖属性LanguageProperty FrameworkElement.LanguageProperty.OverrideMetadata( typeof(Fr ...