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. HTML-文本域属性设置

    1.设置文本域的字体 <TEXTAREA STYLE="font-size:9pt;font-family:verdana;color:#333333">输入内容< ...

  2. Elasticsearch 因拷贝多余的jar到lib库导致无法启动的问题

    因为需要测试,无意中拷贝了一个netty-buffer-4.1.16.Final.jar包放到es的lib目录下,晚上回家启动es的时候发现启动不起来了.检查日志发现如下错误. 其中有一句关键语句 C ...

  3. 博客主题皮肤探索-GitHub和jsdelivr的使用

    有个前言 本萌并不会前端相关的知识,一切都是自己慢慢摸索出来的,如果存在代码方面的不足,请尽快告诉我~~~ 使用一个主题 目前我博客使用是 https://www.cnblogs.com/bndong ...

  4. QRegExp

    这段代码会越界,百思不得七姐(过了N久时间 之后^^)原来是把i写成了1  --! //#if 0 QRegExp re1("AT+CGATT?"); QRegExp re2(&q ...

  5. MySQL授权命令grant的详细使用方法

    2019-01-07 转自 https://www.cnblogs.com/crxis/p/7044582.html 本文实例,运行于 MySQL 5.0 及以上版本. MySQL 赋予用户权限命令的 ...

  6. NEXIQ 125032 USB Link Diesel Truck Diagnose Interface Introduction

    What are the features of nexiq usb link? 1.Compatible with applications that diagnose engines, trans ...

  7. android面试注意点

    Java 基础知识 面向对象的基本思想 Object类相关:Object类的几个关键函数.String涉及到的常量池概念,序列化 & 反序列化. 重要关键字:final.static. 内部类 ...

  8. C#文件上传编码乱码

    又遇到文件编码乱码的事情,这回稍微有些头绪,但是还是花了很多时间去解决. 场景:上传csv文件,导入到数据库.上传文件的编码不定,需要转成unicode存储. 问题点:需要解决判断上传文件的编码. 关 ...

  9. zookeeper 选举机制 和 eruake

    zookeeper简介: 在分布式环境中,多个服务之间协调一致.有提供分布式锁.服务配置.实现分布式领域CAP(consistency一致性,Availiablity高可用,patition tolr ...

  10. jQuery示例 | 分级菜单

    <!DOCTYPE html> Title .header{ background-color: black; color: wheat; } .content{ min-height: ...