一道google的面试题(据说)
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的面试题(据说)的更多相关文章
- 【Android】一道Android OpenGL笔试题
一道Android OpenGL笔试题 SkySeraph May. 5th 2016 Email:skyseraph00@163.com 更多精彩请直接访问SkySeraph个人站点:www.sky ...
- 解析js中作用域、闭包——从一道经典的面试题开始
如何理解js中的作用域,闭包,私有变量,this对象概念呢? 就从一道经典的面试题开始吧! 题目:创建10个<a>标签,点击时候弹出相应的序号 先思考一下,再打开看看 //先思考一下你会怎 ...
- google的面试题(三维动态规划的范例)——(87)Scramble String
转:http://www.cnblogs.com/easonliu/p/3696135.html 分析:这个问题是google的面试题.由于一个字符串有很多种二叉表示法,貌似很难判断两个字符串是否可以 ...
- 一道SQL的面试题之联想
一道SQL的面试题之联想 本人工作在一家小型的民营企业,主要从事业务系统的日常维护,二次开发,菜鸟一枚.周五经理准备面试两个开发人员,据简历,都还比较不错,让经理产生了想法,于是准备了一套面试题目,给 ...
- 一道简单的面试题,难倒各大 Java 高手!
Java技术栈 www.javastack.cn 优秀的Java技术公众号 最近栈长在我们的<Java技术栈知识星球>上分享的一道 Java 实战面试题,很有意思,现在拿出来和大家分享下, ...
- 一道 google曾出过的笔试题:编程实现对数学一元多项式的相加和相乘操作(1)
数学中一元n次多项式可表示成如下的形式: Pn(x)=p0+p1x+p2x^2+…+pnx^n (最多有 n+1 项,n +1 个系数唯一确定她) (1)请设计一套接口用以表示和操 ...
- 一道Google面试题——基数排序思想
题目描述: 一个大小为n的数组,里面的数都属于范围[0,n-1],有不确定的重复元素,找到至少一个重复元素,要求O(1)空间和O(n)时间. 算法分析: 这个题目要求用O(n)的时间复杂度,这意味着只 ...
- 一道google面试题
输入n,把1-n分成两个和相等的子集,有多少种分法 想了个dp,直接背包也行 #include <iostream> #include <cstdio> using names ...
- 一道google面试题(dp)
输入n,把1-n分成两个和相等的子集,有多少种分法 想了个dp,直接背包也行 #include <iostream> #include <cstdio> using names ...
随机推荐
- thinkphp3.2.3----图片上传并生成缩率图
public function uploadify(){ if(!IS_POST){ $this->error('非法!'); } $upload = $this->_upload(); ...
- threejs的学习笔记
很多时候,我们在开发3d效果的时候,容易搞不清楚x,y,z坐标分别指示哪个方向 在开发threejs的时候可以先把坐标系添加到场景中,起到一个启示作用. js // show axes in the ...
- docker 创建容器的时候的坑
其实这个题目的话,对于我后面陈述的问题发生的本身并没有太多的联系,但是因为是在docker创建容器的操作之内发生的,所以记录以下 因为网上有些文章有些作者喜欢使用git的命令窗体,说实在的,公司里面用 ...
- TabBarIOS
参考:http://blog.csdn.net/wmmhwj/article/details/68483592 import React, { Component } from 'react';imp ...
- Thread类和Runnable接口的比较
Thread和Runnable的联系 Thread类的定义: public class Thread extends Object implements Runnable 联系:从Thread类的定义 ...
- nginx windows 安装为服务.
安装Nginx 下载windows版nginx (http://nginx.org/download/nginx-1.10.0.zip),之后解压到需要放置的位置(D:\xampp\nginx) 将N ...
- git泄露利用脚本
留一下万一之后用得着呢 工作原理 1.解析.git/index文件,找到工程中所有的: ( 文件名,文件sha1 ) 2.去.git/objects/ 文件夹下下载对应的文件 3.zlib解压文件,按 ...
- python3 md5
参考: https://docs.python.org/3/library/hashlib.html?highlight=hashlib#credits https://blog.csdn.net/w ...
- git笔记(三)
详细输出日志 git log --pretty=raw 查看id类型 git cat-file -t fe4c git cat-file -t b36bf6 git cat-file -t b08 ...
- Git 学习之关于版本库
记得在第一次接触代码的时候,当对一些改动不是很确定的时候,我的做法就是在我的电脑上保留多个文件,分别以不同的名字来保存,以便于以后发现某个地方的带动是错误的好做修改,现在想想真是好笑啊. 慢慢的在工作 ...