设为首页 加入收藏

TOP

Search Insert Position
2017-10-12 17:41:05 】 浏览:895
Tags:Search Insert Position

Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.

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

 

 1 int searchInsert(int* nums, int numsSize, int target) {
 2     int i;
 3     
 4     for(i = 0; i < numsSize; i++)
 5         if(nums[i] > target)
 6             break;
 7     if(i == 0)
 8         return i;
 9     if(nums[i-1] == target)
10         return i - 1;
11     else
12         return i;
13 }

 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇C语言--通用类型的swap函数和数组.. 下一篇53. Maximum Subarray

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目