[8.3] Magic Index
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的更多相关文章
- [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 ...
- 算法----Magic Index
给定一个数组 A,如果 某个下标 i, 满足 A[i] = i, 则 i 称为 Magic Index. 现在假设 A 中的元素是递增有序的.且不重复,找出 Magic Index. 更进一步,当数组 ...
- 数组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 ...
- Magic Index 寻找数组中A[i]=i的位置(原题转自微信号待字闺中)
有一个有意思的题目叫做Magic Index:给定一个数组A,其中有一个位置被称为Magic Index,含义是:如果i是Magic Index,则A[i] = i.假设A中的元素递增有序.且不重复, ...
- 待字闺中之Magic Index 分析
给定一个数组A,当中有一个位置被称为Magic Index,含义是:如果i是Magic Index.则A[i] = i. 如果A中的元素递增有序.且不反复,请给出方法,找到这个Magic Index. ...
- The Aggregate Magic Algorithms
http://aggregate.org/MAGIC/ The Aggregate Magic Algorithms There are lots of people and places that ...
- CareerCup All in One 题目汇总 (未完待续...)
Chapter 1. Arrays and Strings 1.1 Unique Characters of a String 1.2 Reverse String 1.3 Permutation S ...
- hdu 4150 Powerful Incantation
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4150 Powerful Incantation Description Some dangerous ...
- CareerCup All in One 题目汇总
Chapter 1. Arrays and Strings 1.1 Unique Characters of a String 1.2 Reverse String 1.3 Permutation S ...
随机推荐
- 前端构建工具的用法—grunt、gulp、browserify、webpack
随着前端项目的飞速发展,项目越来越大.文件越来越多,前端工程化的工具也越来越多.下面介绍目前最流行的四种构建工具——grunt.gulp.browserify.webpack 所有的构建工具都是基于N ...
- U3D学习笔记1: HelloWorld
Unity 版本: 5.3.5.f1 Hello World工程 1.新建工程 HelloWorld U3D可选2D和3D游戏 2.新建C#脚本文件 在project栏的assets目录右键-&g ...
- 基础知识《十》java 异常捕捉 ( try catch finally ) 你真的掌握了吗?
本文转载自 java 异常捕捉 ( try catch finally ) 你真的掌握了吗? 前言:java 中的异常处理机制你真的理解了吗?掌握了吗?catch 体里遇到 return 是怎么处理 ...
- jQuery的select相关操作
例: <select class="selector" id="selector"></select> 1.设置value为pxx的项选 ...
- Mysql binlog 安全删除
简介: 如果你的 Mysql 搭建了主从同步 , 或者数据库开启了 log-bin 日志 , 那么随着时间的推移 , 你的数据库 data 目录下会产生大量的日志文件 shell > ll /u ...
- 使用Slf4j集成Log4j2构建项目日志系统的完美解决方案
一.背景 最近因为公司项目性能需要,我们考虑把以前基于的log4j的日志系统重构成基于Slf4j和log4j2的日志系统,因为,使用slf4j可以很好的保证我们的日志系统具有良好的兼容性,兼容当前常见 ...
- MySQL延迟复制--percona-toolkit和MASTER TO MASTER_DELAY
为了数据的安全,有的时候数据库需要延迟备份,这里说下两种延迟备份的方法. 一.借助工具. 实现环境: 192.168.189.143 (mysql主库) 192.168.189.144 (mysql备 ...
- cout 格式化输出
一直习惯于C语言的printf函数来打印,突然有一天要用cout来打印,发现有点不适应. 原来cout也是有格式化输出的. 首先要引入头文件 #include<iostream> // 在 ...
- hive 复杂类型
hive提供一种复合类型的数据 struct:可以使用"."来存取数据 map:可以使用键值对来存取数据 array:array中存取的数据为相同类型,其中的数据可以通过下表获取数 ...
- Microsoft VS 2008 过期解决方法破解方法
Microsoft VS 2008 过期解决方法电脑上的Microsoft Visual Studio 2008 Team System 试用版提示离过期还有**天.于是百度,搜索结果大多是以下两种解 ...