class Solution { public: int search(vector<int>& nums, int target) { int left=0;int right=nums.size()-1; while(left<=right){ int mid=left+(right-left)/2; if(target==nums[mid])return mid; else if(nums[mid]>=nums[right]){ if(target>=nums[left]&&target<=nums[mid])right=mid-1; else left=mid+1; } else { if(target<=nums[right]&&target>nums[mid])left=mid+1; else right=mid-1; } } return -1; } };
这篇博客说的很好了,基本上是我想的
https://www.cnblogs.com/grandyang/p/4325648.html
Runtime:
8 ms, faster than 100.00% of C++ online submissions for Search in Rotated Sorted Array.
Memory Usage:
9.7 MB, less than 94.15% of C++ online submissions for Search in Rotated Sorted Array.
给力哦,leetcode可以看到空间大小