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的更多相关文章

  1. Careercup - Google面试题 - 5732809947742208

    2014-05-03 22:10 题目链接 原题: Given a dictionary, and a list of letters ( or consider as a string), find ...

  2. Careercup - Google面试题 - 5085331422445568

    2014-05-08 23:45 题目链接 原题: How would you use Dijkstra's algorithm to solve travel salesman problem, w ...

  3. Careercup - Google面试题 - 4847954317803520

    2014-05-08 21:33 题目链接 原题: largest number that an int variable can fit given a memory of certain size ...

  4. Careercup - Google面试题 - 6332750214725632

    2014-05-06 10:18 题目链接 原题: Given a ,) (,) (,), (,) should be returned. Some suggest to use Interval T ...

  5. Careercup - Google面试题 - 5634470967246848

    2014-05-06 07:11 题目链接 原题: Find a shortest path ,) to (N,N), assume is destination, use memorization ...

  6. Careercup - Google面试题 - 5680330589601792

    2014-05-08 23:18 题目链接 原题: If you have data coming in rapid succession what is the best way of dealin ...

  7. Careercup - Google面试题 - 5424071030341632

    2014-05-08 22:55 题目链接 原题: Given a list of strings. Produce a list of the longest common suffixes. If ...

  8. Careercup - Google面试题 - 5377673471721472

    2014-05-08 22:42 题目链接 原题: How would you split a search query across multiple machines? 题目:如何把一个搜索que ...

  9. Careercup - Google面试题 - 6331648220069888

    2014-05-08 22:27 题目链接 原题: What's the tracking algorithm of nearest location to some friends that are ...

随机推荐

  1. .NET中使用log4net

    一,加载log4net引用 下载log4net.dll,我们这里使用的是.NET2.0 下载地址:http://files.cnblogs.com/gosky/log4net-1.2.13-bin-n ...

  2. ado.net工厂模式DbProviderFactories

    DbProviderFactory f = DbProviderFactories.GetFactory(System.Configuration.ConfigurationManager.Conne ...

  3. 判断文件夹下是否存在txt格式的文本文件

    判断D盘下是否存在txt类型的文件 string p_Path="D:\\"; bool IsHaveTxt() { DirectoryInfo foldinfo = new Di ...

  4. 分析MapReduce执行过程

    分析MapReduce执行过程 MapReduce运行的时候,会通过Mapper运行的任务读取HDFS中的数据文件,然后调用自己的方法,处理数据,最后输出. Reducer任务会接收Mapper任务输 ...

  5. Nginx 403 forbidden的解决办法

    Nginx 403 forbidden的解决办法. 常见的,引起nginx 403 forbidden有二种原因,一是缺少索引文件,二权限问题. 1.缺少index.html或者index.php文件 ...

  6. [Linux] Ubuntu Server 12.04 LTS 平台上搭建WordPress(Nginx+MySQL+PHP) Part IV

    接下来我们去下载 WorePress 用最新的 3.7.1 下载地址是:http://cn.wordpress.org/wordpress-3.7.1-zh_CN.zip 我们先建立一个文件夹 /va ...

  7. 负载均衡 IO etc.

    IO多路复用之epoll总结 ↑2013年8月21日11:49:44 EPOLL下的accept(不错的blog) 关于TCP封包.粘包.半包 linux内核TCP相关参数解释 ↑2013年8月14日 ...

  8. 等价表达式 (codevs 1107)题解

    [问题描述] 明明进了中学之后,学到了代数表达式.有一天,他碰到一个很麻烦的选择题.这个题目的题干中首先给出了一个代数表达式,然后列出了若干选项,每个选项也是一个代数表达式,题目的要求是判断选项中哪些 ...

  9. ios 异步处理耗时操作

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_asy ...

  10. java-servlet-jsp

    =========================servletjavax.servlet    javax.servlet.http    javax.annotation    javax.ser ...