81. Search in Rotated Sorted Array II

和1相比就是多了一个可能,就是截断以后两头一样,所以如果两头是一样的,就往前移动到不一样的地方就好了

 1     public boolean search(int[] nums, int target) {
 2         if(nums == null || nums.length == 0) {
 3             return false;
 4         }
 5         int left = 0;
 6         int right = nums.length-1;
 7         while(left <= right) {
 8             int mid = left+(right-left)/2;
 9             if(nums[mid] == target) {
10                 return true;
11             } else if(nums[mid] > nums[left]) {
12                  if(target >= nums[left] && target < nums[mid]) {
13                      right = mid-1;
14                  } else {
15                      left = mid+1;
16                  }
17             } else if(nums[mid] < nums[left]) {
18                 if(target > nums[mid] && target <= nums[right]) {
19                     left = mid+1;
20                 } else {
21                     right = mid-1;
22                 }
23             } else {
24                 left++;
25             }
26         }
27         return false;
28     }

读了23,24两行

posted @ 2016-04-03 05:21  warmland  阅读(138)  评论(0编辑  收藏  举报