zoukankan      html  css  js  c++  java
  • [Leetcode 88] 33 Search in Rotated Sorted Array

    Problem:

    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.

    Analysis:

    The problem is that the monotic property is broken after shiftting the array. Example is as follows:

    Original Array:  1 2 3 4 5 6 7 8 9 10

    Shifted Array1: 7 8 9 10 1 2 3 4 5 6

    Shifted Array2: 5 6 7 8 9 10 1 2 3 4

    For shifted array 1, the mid is 1 and for shifted array 2, the mid is 9. If now we search 10 in the two arrays, Which side should we go ? Both of 1 and 9 are less than 10, so it seems we need to go the left part of the array. But this is not ture for array2. And we also can decied which part to go with the addition information of A[0] and A[e].

    One way I can think of is if we encount a sorted part of the array, then we use binary search. But if the part of the array is shifted, then we can only search both sides of the array.

    Code:

    Naive version:

     1 class Solution {
     2 public:
     3     int search(int A[], int n, int target) {
     4         // Start typing your C/C++ solution below
     5         // DO NOT write int main() function
     6         int s = 0, e = n-1;
     7         
     8         return searchHelper(A, s, e, target);
     9     }
    10     
    11 private:
    12     
    13     int searchHelper(int A[], int s, int e, int t) {
    14         if (s <= e) {
    15             int m = (s + e) / 2;
    16             
    17             if (A[m] == t)
    18                 return m;
    19             
    20             if (A[s] < A[e]) {
    21                 if (A[m] > t)
    22                     return searchHelper(A, s, m-1, t);
    23                 else
    24                     return searchHelper(A, m+1, e, t);
    25             } else {
    26                 int res1 = searchHelper(A, s, m-1, t);
    27                 int res2 = searchHelper(A, m+1, e, t);
    28                 
    29                 return (res1 == -1) ? ((res2 == -1)? -1:res2): res1;
    30             }
    31         }
    32         
    33         return -1;
    34     }
    35 };
    View Code

    More Sophiscated Version:

     1 class Solution {
     2 public:
     3     int search(int A[], int n, int target) {
     4         // Start typing your C/C++ solution below
     5         // DO NOT write int main() function
     6         int s = 0, e = n-1;
     7         
     8         while (s <= e) {
     9             int m = (s + e) / 2;
    10             
    11             if (A[m] == target)
    12                 return m;
    13             else if (A[m] < target) {
    14                 if (A[m] <= A[e]) {
    15                     if (A[e] < target)
    16                         e = m-1;
    17                     else
    18                         s = m+1;
    19                 } else {
    20                     s = m+1;
    21                 }
    22             } else { // A[m] > target
    23                 if (A[m] >= A[s]) {
    24                     if (A[s] > target)
    25                         s = m + 1;
    26                     else
    27                         e = m - 1;
    28                 } else {
    29                     e = m-1;
    30                 }
    31                 
    32             }
    33             
    34         }
    35         
    36         return -1;
    37     }
    38 };
    View Code
  • 相关阅读:
    MySQL的事务和视图
    MySQL中的常用函数
    高级查询(一)
    街道管理系统
    深入.NET平台和C#编程的错题
    appium python下的API方法
    Charles 使用教程
    appium,iOS下,ios_predicate等定位方式
    Linux -常用命令
    python-列表以及相关函数认识
  • 原文地址:https://www.cnblogs.com/freeneng/p/3216872.html
Copyright © 2011-2022 走看看