魔术索引。 在数组A[0...n-1]中,有所谓的魔术索引,满足条件A[i] = i。给定一个有序整数数组,编写一种方法找出魔术索引,若有的话,在数组A中找出一个魔术索引,如果没有,则返回-1。若有多个魔术索引,返回索引值最小的一个。
示例1:
输入:nums = [0, 2, 3, 4, 5]
输出:0
说明: 0下标的元素为0
示例2:
输入:nums = [1, 1, 1]
输出:1
class Solution {
public int findMagicIndex(int[] nums) {
int ans = 0;
while(ans < nums.length) {
if(nums[ans] == ans) {
return ans;
// 如果当前值大于下标,那么从当前位置ans到 nums[ans] - 1一定不符合.
} else if (nums[ans] > ans) {
ans = nums[ans];
} else {
ans++;
}
}
return -1;
}
}
// 遍历
class Solution {
public int findMagicIndex(int[] nums) {
for(int i = 0; i < nums.length; i++) {
if(i == nums[i])
return i;
}
return -1;
}
}
提示:
nums长度在[1, 1000000]之间
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/magic-index-lcci
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。