A magic index in an array A[0...n-1] is defined to be an index such that A[i] = i. Given a sorted array of distinct integers, write a method to find a magic index, if one exists, in array A.

FOLLOW UP

What if the values are not distinct?

    public int getMagicIndex(int[] inputs) {
return helper(0, inputs.length - 1, inputs);
} private int helper(int start, int end, int[] inputs) {
if(end < start)
return -1;
int mid = (end - start) / 2 + start;
if(mid == inputs[mid]) {
return mid;
} else if (mid > inputs[mid]) {
return helper(mid + 1, end, inputs);
} else {
return helper(start, mid - 1, inputs);
}
}

Follow Up: 看答案的

    /**
*
* A magic index in an array A[0...n-1] is defined to be an index such that A[i] = i.
* Given a sorted array of distinct integers, write a method to find a magic index, if one exists, in array A.
*
* FOLLOW UP
* What if the values are not distinct?
*
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] inputs = {-11, -9, -3, -3, 4, 6, 7, 8, 9, 10, 11, 12, 13, 16};
System.out.println(getMagicIndex(inputs));
} public static int getMagicIndex(int[] inputs) {
return helper(0, inputs.length - 1, inputs);
} private static int helper(int start, int end, int[] inputs) {
if(end < start)
return -1; int midIndex = (end - start) / 2 + start;
int midValue = inputs[midIndex]; if(midIndex == midValue) {
return midIndex;
} else {
// Search left
int leftIndex = Math.min(midIndex - 1, midValue);
int left = helper(start, leftIndex, inputs);
if(left > 0) {
return left;
}
// Search right
int rightIndex = Math.max(midIndex + 1, midValue);
return helper(rightIndex, end, inputs);
}
}

[8.3] Magic Index的更多相关文章

  1. [CareerCup] 9.3 Magic Index 魔法序号

    9.3 A magic index in an array A[0.. .n-1] is defined to be an index such that A[i] = i. Given a sort ...

  2. 算法----Magic Index

    给定一个数组 A,如果 某个下标 i, 满足 A[i] = i, 则 i 称为 Magic Index. 现在假设 A 中的元素是递增有序的.且不重复,找出 Magic Index. 更进一步,当数组 ...

  3. 数组Magic Index

    Question A magic index in an array A[1...n-1] is defined to be an index such that A[i] = i. Given a ...

  4. Magic Index 寻找数组中A[i]=i的位置(原题转自微信号待字闺中)

    有一个有意思的题目叫做Magic Index:给定一个数组A,其中有一个位置被称为Magic Index,含义是:如果i是Magic Index,则A[i] = i.假设A中的元素递增有序.且不重复, ...

  5. 待字闺中之Magic Index 分析

    给定一个数组A,当中有一个位置被称为Magic Index,含义是:如果i是Magic Index.则A[i] = i. 如果A中的元素递增有序.且不反复,请给出方法,找到这个Magic Index. ...

  6. The Aggregate Magic Algorithms

    http://aggregate.org/MAGIC/ The Aggregate Magic Algorithms There are lots of people and places that ...

  7. CareerCup All in One 题目汇总 (未完待续...)

    Chapter 1. Arrays and Strings 1.1 Unique Characters of a String 1.2 Reverse String 1.3 Permutation S ...

  8. hdu 4150 Powerful Incantation

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4150 Powerful Incantation Description Some dangerous ...

  9. CareerCup All in One 题目汇总

    Chapter 1. Arrays and Strings 1.1 Unique Characters of a String 1.2 Reverse String 1.3 Permutation S ...

随机推荐

  1. ubuntu14.04环境下spyder的安装

    在ubuntu14.04系统中,默认在/usr/lib目录下安装了python2.7.6和python3.4.3,在该环境下安装spyder,然后使其链接到python3.4.3. 首先安装为pyth ...

  2. Android 常用开发工具以及Mac常用软件

    Android 常用的开发工具记录.其中包括AndroidStudio(IDEA)插件.Mac 上好用的软件以及国内知名Android开发者博客等. Android Studio 插件 codota ...

  3. 坑爹的微信支付v3,其实没有那么坑

    http://www.cnblogs.com/zskbll/p/wxpay.html 研究微信开发一年多了,每个新接口,都会第一时间进行研究.微信支付开放很久,一直没机会接触到支付接口,等了好久终于从 ...

  4. CSS3学习基本记录

    CSS3 边框 border-radius: 圆角 border-radius: 15px 50px 70px 100px; 左上 右上 右下 左下 box-shadow:阴影 box-shadow: ...

  5. (转载) 利用国内的镜像,加速PIP下载

    国内源: 新版ubuntu要求使用https源,要注意. 清华:https://pypi.tuna.tsinghua.edu.cn/simple 阿里云:http://mirrors.aliyun.c ...

  6. [转载]js中return的用法

    一.返回控制与函数结果,语法为:return 表达式; 语句结束函数执行,返回调用函数,而且把表达式的值作为函数的结果 二.返回控制,无函数结果,语法为:return;  在大多数情况下,为事件处理函 ...

  7. docker 初探之简单安装 ----Windows10

    报错一 $ docker run hello-world Unable to find image 'hello-world:latest' locally Pulling repository do ...

  8. Python AES - base64 加解密

    首先python引用AES加密 from Crypto.Cipher import AES 需要先安装  Crypto  模块, 可以使用 easy_install 进行安装   会自动去官网进行搜索 ...

  9. Android搜索功能的案例,本地保存搜索历史记录......

    开发的APP有一个搜索功能,并且需要显示搜索的历史记录,我闲暇之余帮她开发了这个功能,现把该页面抽取成一个demo分享给大家. 实现效果如图所示:  本案例实现起来很简单,所以可以直接拿来嵌入项目中使 ...

  10. jquery 替换img 属性

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...