35_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

在一个顺序数组中找某个值x的位置。若x存在该数组中,则返回x在该数组的位置i;若不存在,则把x按照顺序放进该数组并返回此时x的位置。

顺序查找:

int searchInsert(int* nums, int numsSize, int target) {
    int result = 0;
    for(int i = 0; i < numsSize; i++)
    {
        if(nums[i] == target)
        {
            result = i;
            break;
        }
        else if(nums[i] < target)
        {
            if(i < numsSize - 1)
            {
                continue;
            }
            else
            {
                result = i + 1;
                break;
            }
        }
        else//target < nums[i]
        {
            result = i;
            break;
        }
    }

    return result;
}

由于该数组是顺序的,二分查找法可能速度更快

二分查找:

int searchInsert(int* nums, int numsSize, int target) {
    int cursor = 0;
    int low = 0;
    int high = numsSize - 1;

    if(low == high)
    {
        if(nums[0] >= target)
        {
            return 0;
        }
        else
        {
            return 1;
        }
    }

    while (low <= high)
    {
        cursor = (low + high) / 2;
        if(nums[cursor] == target)
        {
            return cursor;
        }
        else if(nums[cursor] > target)
        {
            high = cursor - 1;
            continue;
        }
        else
        {
            low = cursor + 1;
            continue;
        }
    }

    cursor = (low + high) / 2;

    if(nums[cursor] > target)
    {
        return cursor;
    }
    else
    {
        return cursor + 1;
    }

}
时间: 2024-08-05 19:32:33

35_Search Insert Position的相关文章

LeetCode——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

[LeetCode] Search Insert Position [21]

题目 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]

[leetcode]_Search Insert Position

题目:查找元素target插入一个数组中的位置. 代码: public int searchInsert(int[] A, int target) { int len = A.length; int i; for(i = 0 ; i < len ; i++){ if(target <= A[i]) break; } return i; } 简单的让人难以置信.吼吼~ [leetcode]_Search Insert Position,布布扣,bubuko.com

【leetcode】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,

LeetCode: 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,

Search Insert Position (LeetCode)

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 →

【LeetCode】Search Insert Position (2 solutions)

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,

[LeetCode] 035. Search Insert Position (Medium) (C++)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Search Insert Position (Medium) 链接: 题目:https://leetcode.com/problems/search-insert-position/ 代码(github):https://github.com/illuz/leetcode 题意: 要把一个数有序插入到一

LeetCode: Search Insert Position [034]

[题目] 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,