Skip to content
This repository has been archived by the owner on Feb 14, 2023. It is now read-only.

面试题 08.03. 魔术索引 #32

Open
AmelloAster opened this issue Jul 31, 2020 · 2 comments
Open

面试题 08.03. 魔术索引 #32

AmelloAster opened this issue Jul 31, 2020 · 2 comments
Labels
Easy 史莱姆 Finished 经验+1 Leetcode daily topic 每日药丸 世尘 世界的尘埃哒 金金 Code is life

Comments

@AmelloAster
Copy link
Collaborator

AmelloAster commented Jul 31, 2020

面试题 08.03. 魔术索引

魔术索引。 在数组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
提示:

nums长度在[1, 1000000]之间

解题代码

var findMagicIndex = function (nums) {
        let mid = Math.floor(nums.length / 2);
        let isLeft = nums[mid] > mid;
        let arr = [];
        let index = 0;

        if (isLeft) {
            arr = nums.slice(0, mid)
        } else {
            index = mid;
            arr = nums.slice(mid, nums.length)
        }

        while (true) {
            mid = Math.floor(arr.length / 2);


            if (arr[mid] > mid) {
                if (index - 1 === nums[index - 1]) {
                    return index - 1
                }
                if ((index - mid) === arr[mid]) {
                    return index - mid
                }
                // console.log(mid, arr[mid], index)
                if (index + 1 === arr[mid]) {
                    return index + 1;
                }
                
                index++;
                // if (arr[mid] === nums.findIndex(i => i === arr[mid])) {}
                arr = arr.slice(0, mid)
            }
            if (arr[mid] < mid) {
                index += mid;
                // console.log(mid, arr[mid], index)
                arr = arr.slice(mid, arr.length)
            }


            if (arr[mid] === mid) {
                return arr.findIndex((i, idx) => idx === arr[mid]);
                // return mid;
              //  return nums.findIndex(i => i === arr[mid]);
            }

            if ((mid === 0) && arr[mid] !== mid) {
                // console.log(mid, arr[mid], index)
                return -1
            }
        }
    };

解题思路

在二分的基础上加上左右搜索

解题效率

执行用时:
80 ms, 在所有 JavaScript 提交中击败了33.53%的用户
内存消耗:
39.2 MB, 在所有 JavaScript 提交中击败了50.00%的用户
@AmelloAster AmelloAster added Easy 史莱姆 Finished 经验+1 Leetcode daily topic 每日药丸 金金 Code is life labels Jul 31, 2020
@liujiangs
Copy link
Collaborator

var findMagicIndex = function(nums) { let a = -1 let idx = 0 while(idx < nums.length && nums[idx] != idx) { a = idx idx++ } if(a >= nums.length -1) { a = -2 } return a + 1 };

@liujiangs
Copy link
Collaborator

var findMagicIndex = function(nums) { return nums.findIndex(function(value, index, arr) { return value == index; }) };
解题效率
执行用时:
84 ms, 在所有 JavaScript 提交中击败了23.85%的用户
内存消耗:
38.7 MB, 在所有 JavaScript 提交中击败了100.00%的用户

@liujiangs liujiangs added the 世尘 世界的尘埃哒 label Jul 31, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Easy 史莱姆 Finished 经验+1 Leetcode daily topic 每日药丸 世尘 世界的尘埃哒 金金 Code is life
Projects
None yet
Development

No branches or pull requests

2 participants