您的位置:首页 > 其它

leetcode Search in Rotated Sorted Array II

2013-10-20 22:57 393 查看
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.

I was trapped in this problem using Binary Search. A figure is enough to enumerate all situations. I made some mistakes in classification.



If target > A[start] && target > mid, the interval couldn't be cut off to half.

If target < A[start] && target < mid , the interval couldn't be cut off to half, either.

So the right code is :

class Solution {
 public:
  bool search(int A[], int n, int target) {
    // Note: The Solution object is instantiated only once and is reused by each test case.
    int start = 0, end = n -1, mid = (start + end)/2;
    while (start <= end) {
      mid = (start + end) / 2;
      if (A[mid] == target)
        return true;
      else {
        if (A[start] < A[end]) {
          if (target < A[mid])
            end = mid - 1;
          else
            start = mid +1;
        }
        else {
          if (target > A[start])
            if (target > A[mid])
              ++start;
            else 
              end = mid - 1;
          else if (target == A[start])
            return true;
          else if (target < A[start])
            if (target > A[mid])
              start = mid + 1;
            else
              ++start;
        }
      }
    }
    return false;  
  }
};

A simpler code:

class Solution {
 public:
  int search(int A[], int n, int target) {
    int l = 0, r = n - 1, mid;
    while (l <= r) {
      mid = (l + r) / 2;
      if (A[mid] == target)
        return mid;
      else {
        if (A[l] <= A[r]) {
          if (A[mid] > target)
            r = mid - 1;
          else
            l = mid + 1;
        }
        else if (A[l] > A[r]) {
          if (target == A[l])
            return l;
          else if (A[mid] > A[l] && target > A[mid]) 
            l = mid + 1;
          else if (A[mid] < A[r] && target < A[mid])
            r = mid - 1;
          else
            l = l + 1;
        }
      }
    }
    return -1;
  }
};


However, there are still two problems for the above code:

1. if (A[l] <= A[r]) , maybe 1 3 1 1 1 1, the code is not right.

2. There is still room for binary search, bacause:

l < target < mid drop right

target < l < mid drop left

l < mid < target drop left

mid < target < l drop left

target < mid < l drop right

mid < l < target drop right

So, the right and better code is :

class Solution {
 public:
  bool search(int A[], int n, int target) {
    int l = 0, r = n - 1, mid;
    while (l <= r) {
      mid = (l + r) / 2;
      if (A[mid] == target)
        return true;
      else {
        if (A[l] < A[r]) {
          if (A[mid] < target)
            l = mid + 1;
          else
            r = mid - 1;
        } 
        else {
          if (A[l] == target)
            return true;
          else {
            if (A[l] < target && target < A[mid] || target < A[mid] && A[mid] < A[l] || A[mid] < A[l] && A[l] < target)
              r = mid - 1;
            else if (target < A[l] && A[l] < A[mid] || A[l] < A[mid] && A[mid] < target || A[mid] < target && target < A[l])
              l = mid + 1;
            else
              l = l + 1;
          }
        } 
      }    
    }
    return false;
  }
};


For the first problem:


Search in Rotated Sorted Array

Total Accepted: 17770 Total
Submissions: 62737My Submissions

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?

This code is not right:

class Solution {
 public:
  int search(int A[], int n, int target) {
    int l = 0, r = n - 1, mid;
    while (l <= r) {
      mid = (l + r) / 2;
      if (A[mid] == target)
        return mid;
      else {
        if (A[l] <= A[r]) {
          if (A[mid] < target)
            l = mid + 1;
          else
            r = mid - 1;
        } 
        else {
          if (A[l] == target)
            return l;
          else {
            if (A[l] < target && target < A[mid] || target < A[mid] && A[mid] < A[l] || A[mid] < A[l] && A[l] < target)
              r = mid - 1;
            else if (target < A[l] && A[l] < A[mid] || A[l] < A[mid] && A[mid] < target || A[mid] < target && target < A[l])
              l = mid + 1;
          }
        } 
      }    
    }
    return -1;
  }
};


Think it over....
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: