Created
November 1, 2015 05:16
-
-
Save mengdiwang/07748ae101bc8e578315 to your computer and use it in GitHub Desktop.
searchInsert
This file contains 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 searchInsert(vector<int>& nums, int target) { | |
int lo = 0; | |
int hi = nums.size()-1; | |
while(lo<=hi) | |
{ | |
int mid = ((lo-hi)>>1) + hi; | |
if(nums[mid] == target) | |
return mid; | |
else if(target < nums[mid]) | |
hi = mid-1; | |
else | |
lo = mid+1; | |
} | |
return lo; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment