Created
September 9, 2018 07:14
-
-
Save tamarous/e65807907a254dad492d53431b89fd8a to your computer and use it in GitHub Desktop.
在旋转数组中寻找一个数字
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int search(vector<int>& nums, int target) { | |
int size = nums.size(); | |
int low = 0, high = size; | |
while(low < high) { | |
int mid = low + (high-low)/2; | |
if (nums[mid] == target) { | |
return mid; | |
} | |
if (nums[low] <= nums[mid]) { | |
if (target >= nums[low] && target < nums[mid]) { | |
high = mid; | |
} else { | |
low = mid + 1; | |
} | |
} else { | |
if (target > nums[mid] && target <= nums[high-1]) { | |
low = mid + 1; | |
} else { | |
high = mid; | |
} | |
} | |
} | |
return -1; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment