首页 > 代码库 > [leetcode-35-Search Insert Position]

[leetcode-35-Search Insert Position]

You may assume no duplicates in the array.
Here are few examples.
[1,3,5,6], 5 → 2
[1,3,5,6], 2 → 1
[1,3,5,6], 7 → 4
[1,3,5,6], 0 → 0

int searchInsert(vector<int>& nums, int target)
     {
         int len = nums.size();
         if (len == 0 || nums[0]>=target)return 0;
         for (int i = 1; i < len;i++)
         {
             if (nums[i]==target) return i;
             if (nums[i]>target && target>nums[i - 1])return i;
         }
         return len;
     }

 

[leetcode-35-Search Insert Position]