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

分析

此题目与上一个题目类似都是源于二分查找的变形。

若序列中存在目标元素值,则直接返回其下标,若不存在则返回第一个大于它的元素的下标,只需要在二分搜索算法中稍微修改即可。

AC代码

class Solution {
public:
    int searchInsert(vector<int>& nums, int target) {
        if (nums.size() == 0)
            return 0;
        else if (nums.size() == 1)
        {
            if (nums[0] >= target)
                return 0;
            else
                return 1;
        }
        else{
            return BinarySearch(nums, target);
        }
    }

    int BinarySearch(vector<int> & nums, int target)
    {
        int left = 0, right = nums.size() - 1;

        while (left <= right)
        {
            int mid = (left + right) / 2;
            if (nums[mid] == target)
                return mid;
            else if (nums[mid] < target)
            {
                if (mid == right || nums[mid + 1] > target)
                    return mid + 1;
                else
                    left = mid + 1;
            }
            else{
                if (mid == left || nums[mid - 1] < target)
                    return mid;
                else
                    right = mid - 1;
            }
        }//while

        return -1;
    }
};

GitHub测试程序源码

时间: 2024-10-01 11:43:37

LeetCode(35) Search Insert Position的相关文章

[LC]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. Example 1: Input: [1,3,5,6], 5Output: 2Example

Leetcode 题目整理 Sqrt &amp;&amp; Search Insert Position

Sqrt(x) Implement int sqrt(int x). Compute and return the square root of x. 注:这里的输入输出都是整数说明不会出现 sqrt(7)这种情况,思路一就是应用二分法进行查找.每次给出中间值,然后比对cur的平方与目标值的大小.需要先设定两个变量用来存放左右游标. 这里要考虑一下整型溢出的问题,另外,即使不能开出整数的也要近似给出整数部分,不能忽略. 代码如下: int Solution::mySqrt(int x) { //

(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 OJ_题解(python):035-Search Insert Position【Array】【Easy】

题目: 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(33)Search in Rotated Sorted Array

题目 Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no dup

LeetCode(34)Search for a Range

题目 Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm's runtime complexity must be in the order of O(log n). If the target is not found in the array, return [-1, -1]. For example, Given [5,

leetcode第34题--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(查找插入的位置)

[ 问题: ] 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. 翻译:给你一个排好序的数组和一个目标值,请找出目标值可以插入数组的位置. [ 分析: ]

Leetcode35 Search Insert Position 解题思路(python)

本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第35题,这道题的tag是数组,python里面叫list,需要用到二分搜索法 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 in