Search Insert Position

class Solution {
public:
    int searchInsert(vector<int>& nums, int target) {
        if(nums.size()==1&&nums[0]<target) return 1;
        int high=nums.size()-1,low=0,mid;
        while(low<high)
        {
            mid=(low+high)/2;
            if(nums[mid]<target) low=mid+1;
            else high=mid;
        }
        if(nums[low]<target) return low+1;
        else return low;
    }
};

 

posted on 2016-03-13 15:09  RenewDo  阅读(208)  评论(0编辑  收藏  举报

导航