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++ 必知必会:条款16 指向成员函数的指针并非指针
这一点与指向成员的指针类似,其实现可能更加复杂,因为成员函数同时还存在虚拟函数,需要动态绑定执行动作.当然这种属性是属于函数本身的,此处表达的是指针不涉及函数的属性问题. 1: class shape ...
- Tomcat启动过程(一):从BootStrap到EndPonit
Tomcat版本tomcat6.0.44观察catalina.sh文件,启动入口:org.apache.catalina.startup.Bootstrap,该类main方法执行下面三个方法----- ...
- C# 随机颜色的方法
public string GetRandomColor() { Random RandomNum_First = new Random((int)DateTime.Now.Ticks); // 对于 ...
- 为php安装memcached扩展连接memcached服务器
首先必须完成必要的软件安装,memcached是php连接memcached服务器的php扩展 以前有个叫memcache也是php连接memcached服务器的扩展 php的memcache和mem ...
- apache2下部署node.js应用程序
版本:apache2.2+node.js(v.10.25) 系统环境:ubuntu 12.04(LTS) 32位 因为有些模块并没有开启 所以需要使用以下命令开启该模块 windows下则直接在htt ...
- FaceBook微光闪烁---第三方开源--shimmer-android
Android上的微光闪烁shimmer效果,实现的手段不少,其中比较好的是facebook做的开源库:shimmer-android,其在github上的项目主页是:https://github.c ...
- Google搜索镜像
From:http://www.cnblogs.com/killerlegend/p/3783744.html Date:2014.6.12 By KillerLegend Google 搜索:htt ...
- 7-ZIP实现数据高度压缩
From:http://www.cnblogs.com/killerlegend/p/3746395.html Author:KillerLegend Date:2013.5.22 选中文件,鼠标右键 ...
- Android———从GitHub上下载源码的方法【Written By KillerLegend】
首先声明,本文说的是从GitHub上下载源码而非上传源码! 1:下载tortoisegit,下载地址为: https://code.google.com/p/tortoisegit/wiki/Down ...
- DevExpress BarManager 部分用法
1.创建一个BarManager会默认产生三个菜单.BarManager右键ShowDesignTime enhancements会显示[add]按钮,可对菜单进行编辑. 2.其中比较有用的属性: 选 ...