Skip to content

Instantly share code, notes, and snippets.

@mengdiwang
Created November 1, 2015 05:16
Show Gist options
  • Save mengdiwang/07748ae101bc8e578315 to your computer and use it in GitHub Desktop.
Save mengdiwang/07748ae101bc8e578315 to your computer and use it in GitHub Desktop.
searchInsert
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