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. 一个完整的菜谱客户端(android源码)(有独立后台)

    该源码是自己写的,是一个完整的菜谱类客户端.功能简单比较简单,界面比较丑,自己乱拼接的,只为学习用.功能相对完整,数据来自独立后台,通过http协议获取,全部来自真实数据.代码里面有获取数据的相应ur ...

  2. Ubuntu16.04安装JDK

    转载请注明源出处:http://www.cnblogs.com/lighten/p/6105463.html 1.简单的安装方法 安装JDK的最简单方法应该就是使用apt-get来安装了,但是源一般是 ...

  3. Linux使用有线上网教程

    本人亲测Linux(Ubuntu kylin 14.04)有线上网方法,下面是步骤: 一,运行Terminal(终端),输入  sudo pppoeconf  命令,设置账号和密码后,其他的全选yes ...

  4. LoadCursor 函数

    从可执行文件中载入指定的光标资源,加载到指定的应用实例中 ? 1 2 3 4 5 HCURSOR WINAPI LoadCursor(    _In_opt_ HINSTANCE hInstance, ...

  5. IIS安装错误导致网站访问不了

    如下图,网站正常但就是访问不了,原因是IIS配置不正确,把ASP.NET4.5等相关勾选上就可以了,不要用默认的勾选,要自己手动勾选.

  6. Vue.js学习 Item6 -- Class 与 样式绑定

    数据绑定一个常见需求是操作元素的 class 列表和它的内联样式.因为它们都是 attribute,我们可以用 v-bind 处理它们:只需要计算出表达式最终的字符串.不过,字符串拼接麻烦又易错.因此 ...

  7. css3选择器——导图篇

    css3选择器主要有:基本选择器 , 层次选择器,  伪类选择器 ,  伪元素选择器 , 属性选择器 基本选择器  层次选择器 伪类选择器分为 动态伪类选择器, 目标伪类选择器, 语言伪类选择器, U ...

  8. Android之Selector、Shape介绍

    ------------整理自网络---------------------- <?xml version=”1.0″ encoding=”utf-8″?> <shape xmlns ...

  9. [转]给C++初学者的50个忠告

    1.把C++当成一门新的语言学习(和C没啥关系!真的.):   2.看<Thinking In C++>,不要看<C++变成死相>:   3.看<The C++ Prog ...

  10. 支付宝收款连接 非API

    <a href="https://shenghuo.alipay.com/send/payment/fill.htm?_form_token=mMYOrAXfReOtBBCMmoaK7 ...