class Solution { public: int search(vector<int>& nums, int target) { int Right=0,Left=nums.size()-1,mid; mid = (Right+Left)/2; if(target==nums[Right]) return Right; else if(target==nums[Left]) return Left; while(1) { if(nums[mid]==target) { return mid; } else if(target>nums[mid]) { Right = mid; mid = (Right+Left)/2; if(Right==Left||Right==Left-1||Right-1==Left) break; }else if(target<nums[mid]) { Left = mid; mid = (Right+Left)/2; if(Right==Left||Right==Left-1||Right-1==Left) break; } } return -1; } };
力扣.