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. 新手浅谈Task异步编程和Thread多线程编程

    初学Task的时候上网搜索,看到很多文章的标题都是task取代thread等等相关,我也一直以为task和thread是一类,其实task是.net4.0提出的异步编程,在之前.net1.0有dele ...

  2. Install SQLite

    http://www.tutorialspoint.com/sqlite/sqlite_installation.htm The SQLite is famous for its great feat ...

  3. PHP常用函数和常见疑难问题解答

    PHP常用库函数介绍 一.PHP字符串操作常用函数 1.确定字符串长度  int strlen(string str) 2.比较两个字符串 a. strcmp函数对两个字符串进行二进制安全的比较,并区 ...

  4. 关于delphi XE7中的动态数组和并行编程(第一部分)

    本文引自:http://www.danieleteti.it/category/embarcadero/delphi-xe7-embarcadero/ 并行编程库是delphi XE7中引进的最受期待 ...

  5. C# 程序开始主要是写类和方法 的基本步骤和调用方法

    主程序的使用方式以及调用方法字段.属性.方法 using System; using System.Collections.Generic; using System.Linq; using Syst ...

  6. 关键字替换排除HTML标签属性字符

    解决办法: 1.打开文件e/class/functions.php 2.找到函数 ReplaceKey($newstext,$classid=0) 3.找到替换代码 if(STR_IREPLACE) ...

  7. @Styles.Render

    1.@Styles.Render 在页面上可以用@Styles.Render("~/Content/css") 来加载css 首先要在App_Start 里面BundleConfi ...

  8. 正整数转换成N进制的数组

    给定一个正整数,按照N进制转换成数组元素存储 //给定一个整数,把它转换成按照N进制存储的数组 #include <stdio.h> #include <stdlib.h> # ...

  9. 5.css字体

    下面的用一个表格总结了文本样式中字体的一些设置方法: 属性名 说明 CSS 版本 font-size 设置字体的大小 1 font-variant 设置英文字体是否转换为小型大写 1 font-sty ...

  10. WPF动画 storyboard

    <Window x:Class="StoryBoard.MainWindow" xmlns="http://schemas.microsoft.com/winfx/ ...