Fork me on GitHub

[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;
     }

 

posted @ 2017-06-04 16:17  hellowOOOrld  阅读(127)  评论(0编辑  收藏  举报