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. 题目1042:Coincidence(最长公共子序列)

    问题来源 http://ac.jobdu.com/problem.php?pid=1042 问题描述 给定两个字符串,求其最长公共子序列(LCS). 问题分析 网上是在是太多这类问题的文章了,随便贴一 ...

  2. C#-★★函数★★

    一个较大的程序一般应分为若干个程序块,每一个模块用来实现一个特定的功能.所有的高级语言中都有子程序这个概念,用子程序来实现模块的功能.在C#语言中,子程序的作用是由一个主函数和若干个函数构成.由主函数 ...

  3. 51 Nod 1042 数位dp

    1042 数字0-9的数量 1 秒 131,072 KB 10 分 2 级题   给出一段区间a-b,统计这个区间内0-9出现的次数. 比如 10-19,1出现11次(10,11,12,13,14,1 ...

  4. 高阶篇:4.2.2)DFMEA层级分明的失效模式、失效后果、失效原因

    本章目的:明确失效模式.失效后果.失效原因的定义,分清楚层次关系,完成DFMEA这部分的填写. 1.失效模式,失效后果,失效原因的定义: 这是FEMEA手册第四册中的定义. 1.1 潜在失效模式 (b ...

  5. bzoj3280: 小R的烦恼(最小费用最大流)

    Description 小R最近遇上了大麻烦,他的程序设计挂科了.于是他只好找程设老师求情.善良的程设老师答应不挂他,但是要 求小R帮助他一起解决一个难题.问题是这样的,程设老师最近要进行一项邪恶的实 ...

  6. TensorFlow GPU 的使用

    一.TensorFlow 设备分配 1.设备分配规则 If a TensorFlow operation has both CPU and GPU implementations, the GPU d ...

  7. linux 系统管理(2) 文件或目录数量统计

    统计某文件夹下文件的个数 ls -l |grep "^-"|wc -l 统计某文件夹下目录的个数 ls -l |grep "^d"|wc -l 统计文件夹下文件 ...

  8. Java解析excel文档并以List<T>输出

    /********************************************************工具类start*********************************** ...

  9. Java - 冒泡排序的优化算法(尚学堂第七章数组)

    import java.util.Arrays; public class TestBubbleSort2 { public static void main(String[] args) { int ...

  10. kafka java API的使用

    Kafka包含四种核心的API: 1.Producer API支持应用将数据流发送到Kafka集群的主题 2.Consumer API支持应用从Kafka集群的主题中读取数据流 3.Streams A ...