返回介绍

lcci / 10.03.Search Rotate Array / README_EN

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

10.03. Search Rotate Array

中文文档

Description

Given a sorted array of n integers that has been rotated an unknown number of times, write code to find an element in the array. You may assume that the array was originally sorted in increasing order. If there are more than one target elements in the array, return the smallest index.

Example1:


 Input: arr = [15, 16, 19, 20, 25, 1, 3, 4, 5, 7, 10, 14], target = 5

 Output: 8 (the index of 5 in the array)

Example2:


 Input: arr = [15, 16, 19, 20, 25, 1, 3, 4, 5, 7, 10, 14], target = 11

 Output: -1 (not found)

Note:

  1. 1 <= arr.length <= 1000000

Solutions

Solution 1: Binary Search

We define the left boundary of the binary search as $l=0$ and the right boundary as $r=n-1$, where $n$ is the length of the array.

In each binary search process, we get the current midpoint $mid=(l+r)/2$.

  • If $nums[mid] > nums[r]$, it means that $[l,mid]$ is ordered. If $nums[l] \leq target \leq nums[mid]$, it means that $target$ is in $[l,mid]$, otherwise $target$ is in $[mid+1,r]$.
  • If $nums[mid] < nums[r]$, it means that $[mid+1,r]$ is ordered. If $nums[mid] < target \leq nums[r]$, it means that $target$ is in $[mid+1,r]$, otherwise $target$ is in $[l,mid]$.
  • If $nums[mid] = nums[r]$, it means that the elements $nums[mid]$ and $nums[r]$ are equal. At this time, we cannot determine which interval $target$ is in, we can only decrease $r$ by $1$.

After the binary search ends, if $nums[l] = target$, it means that the target value $target$ exists in the array, otherwise it does not exist.

Note that if initially $nums[l] = nums[r]$, we loop to decrease $r$ by $1$ until $nums[l] \neq nums[r]$.

The time complexity is approximately $O(\log n)$, and the space complexity is $O(1)$. Here, $n$ is the length of the array.

Similar problems:

class Solution:
  def search(self, arr: List[int], target: int) -> int:
    l, r = 0, len(arr) - 1
    while arr[l] == arr[r]:
      r -= 1
    while l < r:
      mid = (l + r) >> 1
      if arr[mid] > arr[r]:
        if arr[l] <= target <= arr[mid]:
          r = mid
        else:
          l = mid + 1
      elif arr[mid] < arr[r]:
        if arr[mid] < target <= arr[r]:
          l = mid + 1
        else:
          r = mid
      else:
        r -= 1
    return l if arr[l] == target else -1
class Solution {
  public int search(int[] arr, int target) {
    int l = 0, r = arr.length - 1;
    while (arr[l] == arr[r]) {
      --r;
    }
    while (l < r) {
      int mid = (l + r) >> 1;
      if (arr[mid] > arr[r]) {
        if (arr[l] <= target && target <= arr[mid]) {
          r = mid;
        } else {
          l = mid + 1;
        }
      } else if (arr[mid] < arr[r]) {
        if (arr[mid] < target && target <= arr[r]) {
          l = mid + 1;
        } else {
          r = mid;
        }
      } else {
        --r;
      }
    }
    return arr[l] == target ? l : -1;
  }
}
class Solution {
public:
  int search(vector<int>& arr, int target) {
    int l = 0, r = arr.size() - 1;
    while (arr[l] == arr[r]) {
      --r;
    }
    while (l < r) {
      int mid = (l + r) >> 1;
      if (arr[mid] > arr[r]) {
        if (arr[l] <= target && target <= arr[mid]) {
          r = mid;
        } else {
          l = mid + 1;
        }
      } else if (arr[mid] < arr[r]) {
        if (arr[mid] < target && target <= arr[r]) {
          l = mid + 1;
        } else {
          r = mid;
        }
      } else {
        --r;
      }
    }
    return arr[l] == target ? l : -1;
  }
};
func search(arr []int, target int) int {
  l, r := 0, len(arr)-1
  for arr[l] == arr[r] {
    r--
  }
  for l < r {
    mid := (l + r) >> 1
    if arr[mid] > arr[r] {
      if arr[l] <= target && target <= arr[mid] {
        r = mid
      } else {
        l = mid + 1
      }
    } else if arr[mid] < arr[r] {
      if arr[mid] < target && target <= arr[r] {
        l = mid + 1
      } else {
        r = mid
      }
    } else {
      r--
    }
  }
  if arr[l] == target {
    return l
  }
  return -1
}
function search(arr: number[], target: number): number {
  let [l, r] = [0, arr.length - 1];
  while (arr[l] === arr[r]) {
    --r;
  }
  while (l < r) {
    const mid = (l + r) >> 1;
    if (arr[mid] > arr[r]) {
      if (arr[l] <= target && target <= arr[mid]) {
        r = mid;
      } else {
        l = mid + 1;
      }
    } else if (arr[mid] < arr[r]) {
      if (arr[mid] < target && target <= arr[r]) {
        l = mid + 1;
      } else {
        r = mid;
      }
    } else {
      --r;
    }
  }
  return arr[l] === target ? l : -1;
}

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

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

发布评论

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