• Leetcode | Search in Rotated Sorted Array I & II


    Search in Rotated Sorted Array I 

    Suppose a sorted array is rotated at some pivot unknown to you beforehand.

    (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).

    You are given a target value to search. If found in the array return its index, otherwise return -1.

    You may assume no duplicate exists in the array.

    Have you been asked this question in an interview?

    二分查找。

    1. 如果A[mid]>A[l],也就是A[l...mid]是递增序。如果target>=A[l]&&target<A[mid],那么target只可能在[l...mid-1]这个区间上,否则在[mid+1...h]这个区间。

    2. 如果A[mid]<=A[l],那么A[mid...h]是递增序。如果target>A[mid] && target<=A[h],那么target只可能在[mid+1...h]这个区间,否则在[l...mid-1]这个区间。

     1 class Solution {
     2 public:
     3     int search(int A[], int n, int target) {
     4         if (n == 0) return -1;
     5         int l = 0, h = n - 1, mid;
     6         
     7         while (l <= h) {
     8             mid = (l + h) / 2;
     9             if (A[mid] == target) return mid;
    10             if (A[mid] >= A[l]) {
    11                 if (target >= A[l] && target < A[mid]) {
    12                     h = mid - 1;
    13                 } else {
    14                     l = mid + 1;
    15                 }
    16             } else {
    17                 if (target > A[mid] && target <= A[h]) {
    18                     l = mid + 1;
    19                 } else {
    20                     h = mid - 1;
    21                 }
    22             }
    23         }
    24         
    25         return -1;
    26     }
    27 };

    Search in Rotated Sorted Array II

    Follow up for "Search in Rotated Sorted Array":
    What if duplicates are allowed?

    Would this affect the run-time complexity? How and why?

    Write a function to determine if a given target is in the array.

    有了重复数。那么前面的情况,只对应于A[mid]>A[l]和A[mid]<A[l]是成立的。对于A[mid]==A[l],我们无法确定该数是在左区间还是右区间,反正一定不会是在A[l]上,所以l++。

    最坏情况就是整个数组都是同一个数,target不在数组中,这样每次都只能l++,算法复杂度是O(n)。

     1 class Solution {
     2 public:
     3     bool search(int A[], int n, int target) {
     4        if (n == 0) return false;
     5         int l = 0, h = n - 1, mid;
     6         
     7         while (l <= h) {
     8             mid = (l + h) / 2;
     9             if (A[mid] == target) return true;
    10             if (A[mid] == A[l]) {
    11                 l++;
    12             } else if (A[mid] > A[l]) {
    13                 if (target >= A[l] && target < A[mid]) {
    14                     h = mid - 1;
    15                 } else {
    16                     l = mid + 1;
    17                 }
    18             } else {
    19                 if (target > A[mid] && target <= A[h]) {
    20                     l = mid + 1;
    21                 } else {
    22                     h = mid - 1;
    23                 }
    24             }
    25         }
    26         
    27         return false;
    28     }    
    29 };
  • 相关阅读:
    Unity模型导入导出
    3D知识补充
    Unity. Navigation和寻路
    Unity坐标系
    Unity材质
    Unity摄像机
    Unity光照
    Unity资源管理与更新
    Unity垃圾回收
    intellij idea 的全局搜索快捷键方法
  • 原文地址:https://www.cnblogs.com/linyx/p/3735551.html
Copyright © 2020-2023  润新知