1. 原题(同事给的)

Max Howell 参加了谷歌的面试,出题人竟然要求 Max Howell 在白板上作出解答,Max Howell 当然愤怒地拒绝了,回家以后马上在微博上跟我们分享他的吐槽:

Google: % of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so fuck off.
看来在白板上作出反转二叉树的解答并不容易呢?那么在 ScriptOJ 有上 OJ 系统会不会更方便一些呢? 假如有这样一个二叉树, / \ / \ / \ / \ / 使用广度优先的原则用数组的表示就是 [, , , , , , , , , , null, null, null, null, null],二叉树中的空位用 null 表示。 进行反转以后会变成 / \ / \ / \ \ / \ 使用广度优先的原则用数组的表示就是 [, , , , , , , null, null, null, null, null, , , ]。 请实现函数 invertTree,它接受一个表示二叉树的数组,返回表示这个反转二叉树的数组。 请注意,提交后提示中显示的 ,,,,,, 表示的是 , , , null, null, , 。

2.1 code

package org.rocky.learn.algorithm;
/**
* Created by admin on 2018/1/10.
*/
public class ConvertArray {
public static Integer[] convertTree(Integer[] from){
int length = from.length;
int height = (int) Math.ceil(log(length, 2));
int should_size = (int) Math.pow(2, height)-1;
Integer[] result = new Integer[should_size];
result = copyArray(from, result);
int index = 0;
for(int i=0; i<height; i++){
int size = (int) Math.pow(2, i);
result = convertArray(result, index, size);
index += size;
}
return result;
} public static Integer[] copyArray(Integer[] from, Integer[] to){
for(int i=0; i<Math.min(from.length, to.length); i++){
to[i] = from[i];
}
return to;
} public static double log(double value, double base){
return Math.log(value)/Math.log(base);
}
public static Integer[] convertArray(Integer[] from, int beginIndex, int size){
int length = from.length;
Integer[] to = new Integer[length];
for (int i=0; i<length; i++){
if(i<beginIndex || i>beginIndex+size-1)
to[i] = from[i];
else
to[i] = from[beginIndex + size -1 -(i-beginIndex)];
}
return to;
} public static void main(String[] args){
Integer[] to = convertTree(new Integer[]{1,2,3,4,5,6});
for(int i=0; i<to.length; i++){
System.out.print(to[i]==null?"":to[i]);
if(i<to.length-1)
System.out.print(",");
}
}
}

2.2 更新

public static Integer[] convertArray(Integer[] array, int beginIndex, int size){
int length = array.length;
if(beginIndex >=0 && beginIndex <= length-1){
int endIndex = beginIndex+size > length ? length : beginIndex + size;
for(int i=beginIndex; i<(endIndex+beginIndex)/2; i++){
int index = endIndex-1-(i-beginIndex);
Integer temp = array[i];
array[i] = array[index];
array[index] = temp;
}
}
return array;
}

3. 总结

写出来的速度反应编程能力, fighting!

一道google的面试题(据说)的更多相关文章

  1. 【Android】一道Android OpenGL笔试题

    一道Android OpenGL笔试题 SkySeraph May. 5th 2016 Email:skyseraph00@163.com 更多精彩请直接访问SkySeraph个人站点:www.sky ...

  2. 解析js中作用域、闭包——从一道经典的面试题开始

    如何理解js中的作用域,闭包,私有变量,this对象概念呢? 就从一道经典的面试题开始吧! 题目:创建10个<a>标签,点击时候弹出相应的序号 先思考一下,再打开看看 //先思考一下你会怎 ...

  3. google的面试题(三维动态规划的范例)——(87)Scramble String

    转:http://www.cnblogs.com/easonliu/p/3696135.html 分析:这个问题是google的面试题.由于一个字符串有很多种二叉表示法,貌似很难判断两个字符串是否可以 ...

  4. 一道SQL的面试题之联想

    一道SQL的面试题之联想 本人工作在一家小型的民营企业,主要从事业务系统的日常维护,二次开发,菜鸟一枚.周五经理准备面试两个开发人员,据简历,都还比较不错,让经理产生了想法,于是准备了一套面试题目,给 ...

  5. 一道简单的面试题,难倒各大 Java 高手!

    Java技术栈 www.javastack.cn 优秀的Java技术公众号 最近栈长在我们的<Java技术栈知识星球>上分享的一道 Java 实战面试题,很有意思,现在拿出来和大家分享下, ...

  6. 一道 google曾出过的笔试题:编程实现对数学一元多项式的相加和相乘操作(1)

    数学中一元n次多项式可表示成如下的形式:  Pn(x)=p0+p1x+p2x^2+…+pnx^n     (最多有 n+1 项,n +1 个系数唯一确定她)      (1)请设计一套接口用以表示和操 ...

  7. 一道Google面试题——基数排序思想

    题目描述: 一个大小为n的数组,里面的数都属于范围[0,n-1],有不确定的重复元素,找到至少一个重复元素,要求O(1)空间和O(n)时间. 算法分析: 这个题目要求用O(n)的时间复杂度,这意味着只 ...

  8. 一道google面试题

    输入n,把1-n分成两个和相等的子集,有多少种分法 想了个dp,直接背包也行 #include <iostream> #include <cstdio> using names ...

  9. 一道google面试题(dp)

    输入n,把1-n分成两个和相等的子集,有多少种分法 想了个dp,直接背包也行 #include <iostream> #include <cstdio> using names ...

随机推荐

  1. php中的date和strtotime函数妙用

    php中的两个常用的日期相关函数date和strtotime,相信大家一定不陌生.但我们平时使用都只是基本功能,什么时间戳变日期格式,日期格式变时间戳. 其实这两个函数还有更深的用法: 1.date函 ...

  2. SpringMVC的JSP页面中中EL表达式不起作用的问题解决

    原文地址:https://blog.csdn.net/sinat_35512245/article/details/53611663

  3. poi操作Excel并修改单元格背景色

    废话不多说,直接来代码!!! 其中标红的才是重点!!! 代码中有时可以不用创建新文件, 如果报错的话可以通过创建新文件来进行操作(懒,没去找报错原因),不过原文件也会被修改. 操作之前做好备份!操作之 ...

  4. vim编辑器基本操作及文件权限,sudo命令等介绍

    一:vim 操作命令,在命令模式下操作 pageup 往上翻页 pagedown 往下翻页 H 移动到屏幕首行 gg 移动光标到文档的首行 前面加数字n表示移动到n行内容 G 移动到文档最后一行/查找 ...

  5. 关于CAS

    CAS就是Compare And Swap. CAS操作是在每一次做修改操作时,并不加锁,而是在修改时比较旧值是否有变化,如果旧值不变就执行修改,如果旧值有变,则修改失败. 使用sql表示就是 upd ...

  6. du及df命令的使用

    在本文中,我将讨论 du 和 df 命令.du 和 df 命令都是 Linux 系统的重要工具,来显示 Linux 文件系统的磁盘使用情况.这里我们将通过一些例子来分享这两个命令的用法. du 命令 ...

  7. Python+Selenium之cannot focus element 解决方法

    有时候刚进入页面输入第一个值时脚本会报错:cannot focus element 贴下我的脚本和解决办法供大家参考 我原本的脚本是: WebDriverWait(driver,15,0.5).unt ...

  8. Struts中Validate()和validateXxx的使用

    Struts中Validate()和validateXxx的使用 学习struts2之后,你会发现validate在之前是没有的!它是怎么实现的呢? validate和validateXxxx都是拦截 ...

  9. HttpUtils 封装类

    作为一个web开发人员,对Http 请求,并不陌生.有时候,我们请求的时候,需要使用代码实现,一般情况,我们使用Apache Jakarta Common 下的子项目.的HttpClient. 可是我 ...

  10. CentOS6.4 安装Redis

    按照下面步骤依次执行1.检查依赖,安装依赖 [root@ecs-3c46 ~]# whereis gcc gcc: /usr/bin/gcc /usr/lib/gcc /usr/libexec/gcc ...