81. Search in Rotated Sorted Array II

Posted zhuangbijingdeboke

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了81. Search in Rotated Sorted Array II相关的知识,希望对你有一定的参考价值。

 1 class Solution 
 2 {
 3 public:
 4     bool search(vector<int>& nums, int target) 
 5     {
 6         int sz=nums.size();
 7         if(sz==0)
 8             return false;
 9         int k=nums[0];
10         if(target==k)
11             return true;
12         int left=0,right=sz-1;
13         while(nums[left]==k&&left<right)
14             left++;
15         while(nums[right]==k&&right>0)
16             right--;
17         while(left<right)
18         {
19             int mid=left+((right-left)>>1);
20             int cur=nums[mid];
21             if(target==cur)
22                 return true;
23             if(nums[left]<=cur)
24             {
25                 if(target>=nums[left]&&cur>target)
26                     right=mid;
27                 else
28                     left=mid+1;
29             }
30             else
31             {
32                 if(target<=nums[right]&&target>cur)
33                     left=mid+1;
34                 else
35                     right=mid;
36             }       
37         }
38         return nums[left]==target;
39     }
40 };
 1 class Solution 
 2 {
 3 public:
 4     bool search(vector<int>& nums, int target) 
 5     {
 6         int left = 0, right =  nums.size()-1, mid;
 7         
 8         while(left<=right)
 9         {
10             mid = (left + right) >> 1;
11             if(nums[mid] == target) return true;
12 
13             // the only difference from the first one, trickly case, just updat left and right
14             if( (nums[left] == nums[mid]) && (nums[right] == nums[mid]) ) {++left; --right;}
15 
16             else if(nums[left] <= nums[mid])
17             {
18                 if( (nums[left]<=target) && (nums[mid] > target) ) right = mid-1;
19                 else left = mid + 1; 
20             }
21             else
22             {
23                 if((nums[mid] < target) &&  (nums[right] >= target) ) left = mid+1;
24                 else right = mid-1;
25             }
26         }
27         return false;
28     }
 1 class Solution 
 2 {
 3 public:
 4     bool search(vector<int>& nums, int target) 
 5     {
 6         int sz=nums.size();
 7         if(sz==0)
 8             return false;
 9         int k=nums[0];
10         int s=nums[sz-1];
11         if(target==k)
12             return true;
13         int flag=(target<k? 1:0);
14         int left=0,right=sz-1;
15         while(nums[left]==k&&left<sz-1)
16             left++;
17         while(nums[right]==k&&right>0)
18             right--;
19         if(left>right)
20             return false;
21         if(flag==0)
22         {
23             while(left<right)
24             {
25                 int mid=left+((right-left)>>1);
26                 int curmid=nums[mid];
27                 int flagmid=(curmid>k? 0:1);
28                 if(flagmid==0)
29                 {
30                     if(curmid==target)
31                         return true;
32                     else if(curmid>target)
33                         right=mid;
34                     else
35                         left=mid+1;
36                 }
37                 else
38                     right=mid-1;
39             }       
40         }
41         else
42         {
43             while(left<right)
44             {
45                 int mid=left+((right-left)>>1);
46                 int curmid=nums[mid];
47                 int flagmid=(curmid>k? 0:1);
48                 if(flagmid==1)
49                 {
50                     if(curmid==target)
51                         return true;
52                     else if(curmid>target)
53                         right=mid;
54                     else
55                         left=mid+1;
56                 }
57                 else
58                     left=mid+1;
59             }       
60         }
61         return nums[left]==target;
62     }
63 };

三种方法都行,判定条件复杂,程序就简单,判定条件简单,程序就复杂,很科学。

以上是关于81. Search in Rotated Sorted Array II的主要内容,如果未能解决你的问题,请参考以下文章

#Leetcode# 81. Search in Rotated Sorted Array II

81. Search in Rotated Sorted Array II

[Leetcode]81. Search in Rotated Sorted Array II

81. Search in Rotated Sorted Array II

81. Search in Rotated Sorted Array II

81. Search in Rotated Sorted Array II