首页 > 代码库 > Search Insert Position

Search Insert Position

思路:注意特殊情况的处理,如[1]:0,[1]:2

class Solution {
public:
    int searchInsert(vector<int>& nums, int target) {
        int index = -1, i = 0;
        for(; i<nums.size(); ++i)
        {
            if(nums[i] == target)
                return i;
                
            if(nums[i] < target)
                index = i;
        }
        
        return index+1;
    }
};

 

Search Insert Position