2015年8月10日 星期一

LEET code--Search Insert Position

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

寫到現在最簡單的一題,這居然不是正確率最高的...
第一次交出去,一次就對XD

反正如果一樣 或是 比目標大 就直接回傳該位置
不然就是最後回傳最後的位置

-----------------


1
2
3
4
5
6
7
8
int searchInsert(int* nums, int numsSize, int target) {
    int i;
    for( i=0 ; i < numsSize ; i++ )
    {
        if(target == nums[i] || nums[i]>target)return i;
    }
    return numsSize;
}

沒有留言:

張貼留言