[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 ...
随机推荐
- redis的一些操作
public class WnsRedisFactory { private static Cache pool = null; private static JedisConnectionFacto ...
- Func
Func<List<int>, string> getStr = (list) => { var returnStr = ""; if (list.A ...
- JS 初级 二(接上)
传送门--http://www.cnblogs.com/Sabo-dudu/p/5786683.html (一) 六. JS 数组类型 数组是一种保存数据的有序列表,数组的每一项可以保存人意类型的数据 ...
- (转载)html中table的使用方法
colspan表示该一储存格向右打通的栏数. rowspan表示该一储存格向下打通的栏数. colspan是表示横向合并单元格,colspan=“3”表示水平合并三个td rowspan是表示竖直 ...
- Xcode的一个简单的UITests
国内写Tests的很少,写UITests的更少了...或许只是我眼见不够开阔.网上那么几篇都是Swift的.这里给个简单的OC. - (void)viewDidLoad { [super viewDi ...
- Entity Framework 简单查询
前言 首先来简单的复习一下如何使用Code First. 第一步还是先建立一个控制台的应用程序,然后通过Nuget添加Entity Framework.那么同时会给packages.config和Ap ...
- Moving Average from Data Stream
Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...
- struts2 No result defined for action XXX and result input
这种错误的原因一般是页面的属性和action里的属性个数.名称.类型不一致造成的 困扰了我一下午的问题,原来是表单中有两个input-text的name属性重复了,然后接受参数的时候就出现了这个错误 ...
- File文件的使用
线程的停止: 1.停止一个线程一般是通过一个变量来控制 2.如果需要停止一个处于一个等待状态的线程,那么需要配合interrupture方法来完成 守护线程:(后台线程):在一个进程中只剩下守护线程, ...
- C# 操作excel单元格居中
C# 操作excel //导出Excel private void ExportExcel(string fileName, System.Data.DataTable myDGV, s ...