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.二分查找时间复杂度O(logn)。

二分查找的基本思想是将n个元素分成大致相等的两部分,去a[n/2]与x做比较,如果x=a[n/2],则找到x,算法中止;如果x<a[n/2],则只要在数组a的左半部分继续搜索x,如果x>a[n/2],则只要在数组a的右半部搜索x.

时间复杂度无非就是while循环的次数!

总共有n个元素,

渐渐跟下去就是n,n/2,n/4,....n/2^k,其中k就是循环的次数

由于你n/2^k取整后>=1

即令n/2^k=1

可得k=log2n,(是以2为底,n的对数)

所以时间复杂度为O(logn)

代码:

class Solution {
public:
    int searchInsert(int A[], int n, int target) {
        int l=0;
        int r=n-1;
        int mid;
        while (l<=r)
        {
            mid=(l+r)/2;
            if(A[mid]>target) r=mid-1;
            else if(A[mid]<target) l=mid+1;
            else return mid;
        }
        if(l==r+1)
            return l;
        else if(r=-1)
            return 0;
        else if(l=n)
            return n;
    }
};

2.一般方法,复杂度O(n),先找出边界,然后就是相等和两数之间的情况。

class Solution {
public:
    int searchInsert(int A[], int n, int target) {
        if(target>A[n-1]) return n;
        if(target<A[0]) return 0;
        for(int i=0;i<n;++i)
        {
            if(A[i]==target) return i;
            if(i<(n-1)&&target>A[i]&&target<A[i+1])
                return i+1;
        }
    }
};
时间: 2024-10-29 03:59:11

Search Insert Position(二分查找)的相关文章

Leetcode 35 Search Insert Position 二分查找(二分下标)

基础题之一,是混迹于各种难题的基础,有时会在小公司的大题见到,但更多的是见于选择题... 题意:在一个有序数列中,要插入数target,找出插入的位置. 楼主在这里更新了<二分查找综述>第一题的解法,比较类似,当然是今天临时写的. 知道了这题就完成了leetcode 4的第二重二分的写法了吧,楼主懒... 1 class Solution { 2 public: 3 int searchInsert(vector<int>& nums, int target) { 4 in

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

leetcode 刷题之路 70 earch 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 35 Search Insert Position(查找插入位置)

题目链接: https://leetcode.com/problems/search-insert-position/?tab=Description 在给定的有序数组中插入一个目标数字,求出插入该数字的下标 由于该数组是已经排好序的数组,可以利用二分查找. 二分查找的返回结果: 1. 当查找的数字在数组中时,返回第一次出现的下标 2. 当查找的数字不存在时,返回 - pos - 1(即 应当插入位置的相反数再减去 参考代码: package leetcode_50; import java.u

LeetCode OJ:Search Insert Position(查找插入位置)

首线是自己一个比较蠢的方法,可能当时没怎么细细的想,大体的思路就是,将vector中元素存放到set中(因为set插入的时候已经排好序了),首先查找,找不到的话在插入,兵器记下插入位置,指针递增到那个地方的时候就找到了那个位置.如果第一次找到那个位置的就直接递增找到那个位置即可,代码见下,很不优雅: 1 class Solution { 2 public: 3 int searchInsert(vector<int>& nums, int target) { 4 set<int&

Leetcode 二分查找 Search Insert Position

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Search Insert Position Total Accepted: 14279 Total Submissions: 41575 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 i

leetcode_35题——Search Insert Position(二分查找)

Search Insert Position Total Accepted: 56150 Total Submissions: 158216My Submissions Question Solution 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

[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

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,