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 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?

这道题定义了一个魔法序号,就是一个数组的序号等于该位置的值的时候,这个序号就是魔法序号,给了我们一个有序数组,让我们来找魔法序号。这里brute force的方法就不提了,因为没啥考察的目的,对于高效的查找方法我们就要首先考虑二分搜索法,首先我们来看这种方法,没啥特别的地方,套用一般的二分查找法的格式即可,参见代码如下:

class Solution {
public:
int getMagicIdx(vector<int> &nums) {
int left = , right = nums.size() - ;
while (left <= right) {
int mid = (left + right) / ;
if (nums[mid] == mid) return mid;
else if (nums[mid] > mid) right = mid - ;
else left = mid + ;
}
return -;
}
};

这道题的Follow up是说如果数组由重复项怎么处理,那么传统的二分搜索法就会失效,因为下列这种情况可能存在:

-10 -5 2 2 3 4 9 12 13
0 1 3 4 5 6 8 9 10

这种情况符合题意,但是左右两边都会出现魔法序号,所以二分查找法会失效。那么我们难道又要用地毯式搜索了么,其实也不必,我们可以用一种类似于二分搜索法的递归方法来解决问题,就拿上面那个例子来说,第一次找到比较完中间点后,由于左右两边都会出现答案,所以我们左右半段要分别递归一下,这里我们可以加一个trick来优化算法,比如要递归左半段时,那么新的右边界就可以设为min(mid - 1, nums[mid]),同理递归右半段时,左边界可以设为max(mid + 1, nums[mid])。还有个小trick,就是如果左半段搜到了答案,那么直接返回即可,不用再搜右半段,因为题目让我们找一个就行了,没说要找出所有的Magic index,参见代码如下:

// Follow up
class Solution {
public:
int getMagicIdx(vector<int> &nums) {
return getMagicIdxDFS(nums, , nums.size() - );
}
int getMagicIdxDFS(vector<int> &nums, int start, int end) {
if (end < start) return -;
int mid = (start + end) / ;
if (mid == nums[mid]) return mid;
int left = getMagicIdxDFS(nums, start, min(mid - , nums[mid]));
if (left >= ) return left;
int right = getMagicIdxDFS(nums, max(mid + , nums[mid]), end);
return right;
}
};

[CareerCup] 9.3 Magic Index 魔法序号的更多相关文章

  1. [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 ar ...

  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. lintcode :Permutation Index 排列序号

    题目: 排列序号 给出一个不含重复数字的排列,求这些数字的所有排列按字典序排序后该排列的编号.其中,编号从1开始. 样例 例如,排列[1,2,4]是第1个排列. 解题: 这个题目感觉很坑的.感觉这只有 ...

  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. The Aggregate Magic Algorithms

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

  9. bootstrap table 分页序号递增问题 (转)

    原文地址:https://segmentfault.com/q/1010000011040346 如题,怎么在bootstrap table中显示序号,序号递增,并且分页有效,等于是每页10条,第2页 ...

随机推荐

  1. IOS组件绑定无效错误

    报错的原因:界面按钮事件没有绑定到源代码或者相关的代码被注释了.比如你的button组件以及绑定到IBOutlet,但是viewcontrol.m上没有相关的代码,就会出现异常.

  2. (传输层)TCP协议

    目录 首部格式数据单位特定注意自动重传请求ARQ具体实现发送缓存接收缓存滑动窗口确认丢失和确认迟到超时重传时间选择报文段的发送时机运输连接发送TCP请求客户端拥塞处理相关概念避免拥塞具体实现TCP 的 ...

  3. Linux与Windows共享文件夹之samba的安装与使用(Ubuntu为例)

    1.写在前面     当你在Windows上安装了一台Linux的虚拟机,你想访问Linux中的文件夹,将虚拟机中的文件复制到Windows主机上,你会怎么做呢?如果这台Linux主机不是虚拟机,而是 ...

  4. 深入剖析 Spring 框架的 BeanFactory

    说到Spring框架,人们往往大谈特谈一些似乎高逼格的东西,比如依赖注入,控制反转,面向切面等等.但是却忘记了最基本的一点,Spring的本质是一个bean工厂(beanFactory)或者说bean ...

  5. Linux Commands intro1

    $((expression)) echo $(2+2) :wrong echo $((2+2))  : right echo Front-{A,B,C}-Back Front-A-Back Front ...

  6. Genesis 2.8-2.12

    And the LORD God planted a garden eastward in Eden; and there he put the man whom he had formed. 9 A ...

  7. 【Android UI设计与开发】3.引导界面(三)实现应用程序只启动一次引导界面

    大部分的引导界面基本上都是千篇一律的,只要熟练掌握了一个,基本上也就没什么好说的了,要想实现应用程序只启动一次引导界面这样的效果,只要使用SharedPreferences类,就会让程序变的非常简单, ...

  8. Stanford机器学习笔记-1.线性回归

    Content: 1. Linear Regression 1.1 Linear Regression with one variable 1.1.1 Gradient descent algorit ...

  9. WinCE应用程序崩溃提示框的处理

    WinCE的开发人员和WinCE设备的用户应该对下面这两个错误不陌生,"Application encountered a serious error and must shut down& ...

  10. Android学习----自适应国际化语言

    [前言] 自适应的知识与编程无关,关键在于配置文件的修改.自适应的内容包括:语言.屏幕.平台.今天就来说一下如何自适应国际化言. internationalization (国际化)简称:i18n,因 ...