- mid不要错误写成begin
- 终止条件 left <=right
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型vector * @param target int整型 * @return int整型 */ int search(vector<int>& nums, int target) { // write code here if(!nums.size()){ return -1; } int begin = 0; int end = nums.size()-1; while(begin<=end){ int mid = (begin+end)/2; if(nums[mid] == target){ return mid; }else if(target< nums[mid]){ if(nums[mid]>nums[begin]){ if(target>=nums[begin]){ end = mid-1; }else{ begin = mid+1; } }else if(nums[mid]<nums[begin]){ end = mid -1; }else{ begin = mid+1; } }else if(target> nums[mid]){ if(nums[mid]>nums[begin]){ begin = mid+1; }else if(nums[mid]<nums[begin]){ if(target>=nums[begin]){ end = mid-1; }else{ begin = mid+1; } }else{ begin = mid+1; } } } return -1; } };