标签:int nbsp span arch array blog pre amp tor
33. Search in Rotated Sorted Array在旋转排序数组中找目标值的位置 int search(vector<int>& nums, int target) { int len=nums.size(); int low=0,high=len-1; while(low<=high){ int mid=(low+high)/2; if(nums[mid]==target){ return mid; } //mid在旋转点左侧 if(nums[mid]<nums[low]){ if(nums[mid]<target&&target<=nums[high]){ low=mid+1; }else{ high=mid-1; } } //mid在旋转点右侧 else if(nums[mid]>nums[high]){ if(nums[mid]>target&&target>=nums[low]){ high=mid-1; }else{ low=mid+1; } } else{ if(nums[mid]<target){ low=mid+1; }else{ high=mid-1; } } } return -1; }
在旋转排序数组之后的数组中找到目标值的位置(很多遍 ,总是晕)
标签:int nbsp span arch array blog pre amp tor
原文地址:http://www.cnblogs.com/muziyun1992/p/6725579.html