返回介绍

lcci / 08.03.Magic Index / README_EN

发布于 2024-06-17 01:04:43 字数 5204 浏览 0 评论 0 收藏 0

08.03. Magic Index

中文文档

Description

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. If not, return -1. If there are more than one magic index, return the smallest one.

Example1:


 Input: nums = [0, 2, 3, 4, 5]

 Output: 0

Example2:


 Input: nums = [1, 1, 1]

 Output: 1

Note:

  1. 1 <= nums.length <= 1000000

Solutions

Solution 1

class Solution:
  def findMagicIndex(self, nums: List[int]) -> int:
    def find(nums, left, right):
      if left > right:
        return -1
      mid = (left + right) >> 1
      left_index = find(nums, left, mid - 1)
      if left_index != -1:
        return left_index
      if nums[mid] == mid:
        return mid
      return find(nums, mid + 1, right)

    return find(nums, 0, len(nums) - 1)
class Solution {
  public int findMagicIndex(int[] nums) {
    int left = 0, right = nums.length - 1;
    return find(nums, left, right);
  }

  private int find(int[] nums, int left, int right) {
    if (left > right) {
      return -1;
    }
    int mid = (left + right) >> 1;
    int leftIndex = find(nums, left, mid - 1);
    if (leftIndex != -1) {
      return leftIndex;
    }
    if (nums[mid] == mid) {
      return mid;
    }
    return find(nums, mid + 1, right);
  }
}
class Solution {
public:
  int findMagicIndex(vector<int>& nums) {
    return find(nums, 0, nums.size() - 1);
  }

  int find(vector<int>& nums, int left, int right) {
    if (left > right) {
      return -1;
    }
    int mid = left + right >> 1;
    int leftIndex = find(nums, left, mid - 1);
    if (leftIndex != -1) {
      return leftIndex;
    }
    if (nums[mid] == mid) {
      return mid;
    }
    return find(nums, mid + 1, right);
  }
};
func findMagicIndex(nums []int) int {
  return find(nums, 0, len(nums)-1)
}

func find(nums []int, left, right int) int {
  if left > right {
    return -1
  }
  mid := (left + right) >> 1
  leftIndex := find(nums, left, mid-1)
  if leftIndex != -1 {
    return leftIndex
  }
  if nums[mid] == mid {
    return mid
  }
  return find(nums, mid+1, right)
}
function findMagicIndex(nums: number[]): number {
  const n = nums.length;
  const find = (l: number, r: number): number => {
    if (l > r || nums[r] < 0) {
      return -1;
    }
    const mid = l + Math.floor((r - l) / 2);
    if (nums[mid] >= l) {
      const res = find(l, mid - 1);
      if (res !== -1) {
        return res;
      }
    }
    if (nums[mid] === mid) {
      return mid;
    }
    return find(mid + 1, r);
  };
  return find(0, n - 1);
}
impl Solution {
  fn find(nums: &Vec<i32>, l: usize, r: usize) -> i32 {
    if l >= r || nums[r - 1] < 0 {
      return -1;
    }
    let mid = l + (r - l) / 2;
    if nums[mid] >= (l as i32) {
      let res = Self::find(nums, l, mid);
      if res != -1 {
        return res;
      }
    }
    if nums[mid] == (mid as i32) {
      return mid as i32;
    }
    Self::find(nums, mid + 1, r)
  }

  pub fn find_magic_index(nums: Vec<i32>) -> i32 {
    Self::find(&nums, 0, nums.len())
  }
}
/**
 * @param {number[]} nums
 * @return {number}
 */
var findMagicIndex = function (nums) {
  return helper(nums, 0, nums.length - 1);
};

function helper(nums, left, right) {
  if (left > right) return -1;
  let mid = Math.floor((left + right) / 2);
  let leftIndex = helper(nums, left, mid - 1);
  if (leftIndex != -1) return leftIndex;
  if (nums[mid] == mid) return mid;
  return helper(nums, mid + 1, right);
}

Solution 2

function findMagicIndex(nums: number[]): number {
  const n = nums.length;
  let i = 0;
  while (i < n) {
    if (nums[i] === i) {
      return i;
    }
    i = Math.max(nums[i], i + 1);
  }
  return -1;
}
impl Solution {
  pub fn find_magic_index(nums: Vec<i32>) -> i32 {
    let n = nums.len();
    let mut i = 0 as i32;
    while (i as usize) < n {
      let num = nums[i as usize];
      if num == i {
        return i;
      }
      i = num.max(i + 1);
    }
    -1
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文