【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,6], 5 → 2
[1,3,5,6], 2 → 1
[1,3,5,6], 7 → 4
[1,3,5,6], 0 → 0

解法一:线性查找(linear search)

class Solution
{
public:
    int searchInsert(int A[], int n, int target)
    {
        int low = 0;
        int high = n-1;
        int mid = (low+high)/2;

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

解法二:二分查找(binary search)

class Solution
{
public:
    int searchInsert(int A[], int n, int target)
    {
        int low = 0;
        int high = n-1;
        int mid = (low+high)/2;

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

【LeetCode】Search Insert Position (2 solutions),布布扣,bubuko.com

时间: 2024-10-25 20:07:28

【LeetCode】Search Insert Position (2 solutions)的相关文章

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

【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] 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 a 2D Matrix

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer of the previous ro

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

leetCode 35.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 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 d

【LeetCode】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 d

【LeetCode】Search in Rotated Sorted Array II 解题报告

[题目] Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. [解析] 相比Search in Rotated Sorted Array,在