[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 ...
随机推荐
- css兼容性大坑
一. \:选择IE6+//区分 IE 8(不实用) .title{ color:yellow\0; color: red\9\0;} \9在 IE 6及其以上都可以识别(但是 IE11不识别 ,IE ...
- This is US 我们的生活
温情暖心剧 看点在于真实,能让人找到不少共鸣像极了平淡而操蛋的生活,不断交织着苦涩和甘甜,柴米油盐.酸甜苦辣.嬉笑打闹.悲欢离合.温情又不尽如人意 this is us,our life 生活有如柠檬 ...
- excel使用技巧
1,如何得到开口向下的大括号 1,插入左右方向的大括号. 2,点击绘制的大括号,旋转出现绿点,旋转90度,即可. 问题2:Excel如何截图 1,选中截图内容,选择复制->复制为图片->( ...
- soft-margin SVM
1. soft-margin SVM的形式 其中ξn表示每个点允许的犯错程度(偏离margin有多远),但是犯错是有代价的,也就是目标函数里面要最小化的.c控制对犯错的容忍程度. 2. 推导soft ...
- office 365 Sharepoint 2013
平台环境: office 365 Sharepoint 2013 操作文件和文件夹 访问文档库的最佳方式是借助在 /_api/web 处可用的 GetFolderByServerRelativeUr ...
- 自动化运维 Expect
Mac 下载:brew install homebrew/dupes/expect expect : -> 自动化脚本工具: 用于处理交互命令; #注意 调用时并不是使用的 /bin/b ...
- GitLab使用
版本:GitLab Community Edition 8.9.9 1.配置权限 public,private developer,master,owner project groups 2.ssh免 ...
- sha512散列(C语言)
/** * \file sha4.h * * \brief SHA-384 and SHA-512 cryptographic hash function * * Copyright (C) 2006 ...
- 【JavaScript】--ajax
1 什么是AJAX AJAX(Asynchronous Javascript And XML)翻译成中文就是“异步Javascript和XML”.即使用Javascript语言与服务器进行异步交互,传 ...
- JS获取浏览器窗口大小 获取屏幕,浏览器,网页高度宽度
网页可见区域宽:document.body.clientWidth 网页可见区域高:document.body.clientHeight 网页可见区域宽:document.body.offsetWid ...