码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode] Search in Rotated Sorted Array

时间:2014-06-19 06:46:36      阅读:303      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   tar   color   

循环有序 一共有以下两种情况

  第一种

    /

  /

 /

/   

     / 

    / 

条件: (A[mid] >= A[low]) ,low~mid 二分,mid~high 递归

第二种

  / 

 /

        /

      /  

     /

    / 

条件: (A[mid] < A[low]) ,low~mid 二分,mid~high 递归

 1 class Solution {
 2 public:
 3     int binarySearch(int A[], int low, int high, int target)
 4     {
 5         int mid = (high+low)/2;
 6         
 7         if(low <= high)
 8         {
 9             if(target == A[mid]) 
10                 return mid;
11             else if(target > A[mid])
12                 return binarySearch(A, mid+1, high, target);
13             else
14                 return binarySearch(A, low, mid-1, target);
15         }
16         return -1;
17     }
18     
19     int search(int A[], int n, int target) 
20     {
21         return search(A, 0, n-1, target);
22     }
23     
24     int search(int A[], int low, int high, int target) 
25     {
26         
27         int mid =  (high+low)/2;
28         
29         if(low > high)
30             return -1;
31         
32         if(A[mid] == target)
33             return mid;
34         
35         if(A[mid] >= A[low]) // the pivot is in the bottom half
36         {
37             if(target >= A[low] && target < A[mid]) // target in the first half
38             {
39                 return binarySearch(A, low, mid-1, target);
40             }
41             else // target in the bottom half
42             {
43                 return search(A, mid+1, high, target);
44             }
45         }
46         else // the pivot is in the first half
47         {
48             if(target >= A[mid+1] && target <= A[high]) // target in the bottom half
49             {
50                 return binarySearch(A, mid+1, high, target);
51             }
52             else // target in the first half
53             {
54                 return search(A, low, mid-1, target);
55             }            
56         }
57     }
58 };

 

迭代:

判读条件和上班一样

 1 class Solution {
 2     public:
 3         int search(int A[], int n, int target) {
 4             int first = 0, last = n;
 5             while (first != last) {
 6                 const int mid = (first + last) / 2;
 7                 if (A[mid] == target)
 8                     return mid;
 9                 if (A[first] <= A[mid]) { // the pivot is in the bottom half
10                     if (A[first] <= target && target < A[mid]) //target in the first half
11                         last = mid;
12                     else
13                         first = mid + 1; //target in the bottom half
14                 } else {// the pivot is in the first half
15                     if (A[mid] < target && target <= A[last-1])//target in the bottom half
16                         first = mid + 1;
17                     else
18                         last = mid;//target in the first half
19                 }
20             }
21             return -1;
22         }
23 };

 

 

[LeetCode] Search in Rotated Sorted Array,布布扣,bubuko.com

[LeetCode] Search in Rotated Sorted Array

标签:style   class   blog   code   tar   color   

原文地址:http://www.cnblogs.com/diegodu/p/3788663.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!