【LeetCode 33】Search in Rotated Sorted Array

Search in Rotated Sorted Array

分段有序的数组,二分查找返回下标,没有返回-1

数组有序之后经过 rotated, 比如:6 1 2 3 4 5  or 5 6 7 8 1 2

思路是:

(1)nums[m] == target  return m

(2)必定有一段是有序的,找到有序段:

 // m to r is ordered 

   if(nums[m] < nums[r]){

             if nums[m] <= targe <= nums[r] , then l = m + 1;

      else: r = m - 1

//   l to m is ordered

  else 

             if nums[l] <= targe <= nums[m] , then  r = m - 1  

      else left = m + 1;

 

posted on 2017-05-23 14:23  暴力的轮胎  阅读(155)  评论(0编辑  收藏  举报

导航