LeetCode——搜索插入位置

LeetCode——搜索插入位置_第1张图片

 int searchInsert(vector& nums, int target) {
        int pos = -1;
        if (target > *(nums.end() - 1))
            return pos = nums.size() ;
        if (target < *nums.begin())
            return pos = 0;
        for (auto item = nums.begin(); item != nums.end(); ++item)
        {
            if (*item <= target)
                if (*item == target)
                    pos = item - nums.begin();
                else 
                    pos = item - nums.begin() +1;
            else 
                break;
        }
        return pos;
    }

 

你可能感兴趣的:(LeetCode)