leetcode-pp / 91alg-6-daily-check

91 算法第六期打卡仓库
28 stars 0 forks source link

【Day 27 】2022-01-07 - 35. 搜索插入位置 #35

Open azl397985856 opened 2 years ago

azl397985856 commented 2 years ago

35. 搜索插入位置

入选理由

暂无

题目地址

https://leetcode-cn.com/problems/search-insert-position

前置知识

暂无

题目描述

给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。

你可以假设数组中无重复元素。

示例 1:

输入: [1,3,5,6], 5
输出: 2
示例 2:

输入: [1,3,5,6], 2
输出: 1
示例 3:

输入: [1,3,5,6], 7
输出: 4
示例 4:

输入: [1,3,5,6], 0
输出: 0
1149004121 commented 2 years ago

35. 搜索插入位置

思路

二分法查找,如果nums[mid]小于target,则答案一定不在mid及其右边,left=mid+1;如果nums[mid]大于target,则答案可能是mid,也可能是mid右边。答案的范围在[0,n],所以右指针指向n。

代码


    var searchInsert = function(nums, target) {
        const n = nums.length;
        let left = 0, right = n;
        while(left < right){
            let mid = (left + right) >> 1;
            if(nums[mid] === target) return mid;
            else if(nums[mid] < target) left = mid + 1;
            else right = mid;
        };
        return left;
    };

复杂度分析

wenlong201807 commented 2 years ago

代码块

// 解法一
var searchInsert = function (nums, target) {
  let head = 0;
  let tail = nums.length;
  let mid = 0;
  while (head < tail) {
    mid = (head + tail) >> 1;
    if (nums[mid] < target) head = mid + 1;
    else {
      tail = mid;
    }
  }
  return head;
};

// 解法二
var searchInsert = function (nums, target) {
  var i = 0;
  while (i <= nums.length) {
    if (nums[i] >= target) {
      return i;
    }
    i++;
  }
  return nums.length;
};

时间复杂度和空间复杂度

Serena9 commented 2 years ago

代码

class Solution(object):
    def searchInsert(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """
        l = 0
        r = len(nums) - 1
        while l <= r:
            mid = (l + r) // 2
            if nums[mid] == target:
                return mid
            elif nums[mid] < target:
                l = mid + 1
            else:
                r = mid - 1
        return l

复杂度分析

时间复杂度:O(logN)

空间复杂度:O(1)

biaohuazhou commented 2 years ago

思路

二分

代码

class Solution {
    public int searchInsert(int[] nums, int target) {
        //如果目标值不存在于数组中,返回它将会被按顺序插入的位置
        //输入: nums = [1,3,5,6], target = 7
        //输出: 4
        int end=nums.length-1,start=0, mid=0;
        while (start<=end){
            mid=(end+start)/2;
            if (nums[mid]==target){
                return mid;
            }
            if (nums[mid]>target){
                end=mid-1;
            }else {
                start=mid+1;
            }
        }
        return start;
    }
}

复杂度分析 时间复杂度: O(logn) 空间复杂度:O(1)

herbertpan commented 2 years ago

思路

基础二分法

  1. 主要是判断两边的边界

    CODE

    class Solution {
    public int searchInsert(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
    
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] == target) {
                return mid;
            } else if (nums[mid] > target) {
                right = mid;
            } else {
                left = mid + 1;
            }
        }
    
        if (nums[left] < target) {
            return left + 1;
        } else {
            return left;
        }
    }
    }

time complexity

  1. time: O(logn)
  2. space: O(1)
now915 commented 2 years ago
var searchInsert = function(nums, target) {
  let l = 0, r = nums.length - 1
  while(l <= r) {
    let m = parseInt((l + r) / 2)
    if (nums[m] < target) {
      l = m + 1
    } else if (nums[m] > target) {
      r = m - 1
    } else {
      return m
    }
  }
  return l
};
RocJeMaintiendrai commented 2 years ago
class Solution {
    public int searchInsert(int[] nums, int target) {
        int left = 0, right = nums.length - 1;
        while(left <= right) {
            int mid = left + (right - left) / 2;
            if(nums[mid] == target) {
                return mid;
            } else if(nums[mid] < target) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        return right + 1;
    }
}

复杂度分析

alongchong commented 2 years ago
class Solution {
    public int searchInsert(int[] nums, int target) {
        int s = 0;
        int f = nums.length - 1;
        int ans = nums.length;

        //

            while (s <= f) {
                int b = (s + f) / 2;
                if (nums[b] >= target) {
                    f = b-1;
                    ans = b;
                } else {
                   s = b+1;

                }
            }
            return ans;
        }
    }
phybrain commented 2 years ago

思路

二分法

代码

class Solution:
    def searchInsert(self, nums: List[int], target: int) -> int:

        r = len(nums)-1
        l = 0
        while l<=r:

            mid = (r+l)//2
            if target==nums[mid]:
                return mid
            elif target<nums[mid]:
                r = mid-1
            elif target>nums[mid]:
                l = mid+1
        return l

复杂度分析

gova-i267 commented 2 years ago

题目

https://leetcode-cn.com/problems/search-insert-position

思路

看到 有序 log n 就自然想到二分法,一道常规的二分,而且 nums.length <= 10^4 直接左右边界相加也不会溢出

Java代码

class Solution {
    public int searchInsert(int[] nums, int target) {
        // 排除边界
        int n = nums.length;
        if (nums[0] > target) return 0;
        if (nums[n-1] < target) return n;
        int left = 0, right = n-1;
        while (left < right) {
            int mid = (left + right) >> 1;
            if (nums[mid] == target) return mid;
            if (nums[mid] > target) right = mid;
            else left = mid+1;
        }
        return left;
    }
}

复杂度分析

wenjialu commented 2 years ago

thought

  in place --> 2 pointer(slow: idx for insert,  fast: walk idx )
       Wrong thought: echange
       not exchange, but replace!!!!!
    eg:
        # 0,1,2,1,1,2,2,3,3,
        #       s
        #               f
        # s=f=1,(if == pass,) if != , nums[s] = nums[f] ,s+=1, f+=1 !not exchange, but replace!!!!!so could compare with previous one.

code

class Solution:
    def removeDuplicates(self, nums: List[int]) -> int:
               if len(nums) <= 1:
            return len(nums)
        slow, fast = 1, 1
        while fast <= len(nums) - 1:
            # print(slow,fast, nums)
            if nums[fast] != nums[fast - 1]:   
                nums[slow] = nums[fast] 
                slow += 1
            fast += 1    
        return slow 

com

Time: O(n) Space:O(1)

ZZRebas commented 2 years ago

思路

二分法,需注意k不在数组内的情况

代码(Python)

def fun(nums,k):
    l=0
    r=len(nums)-1
    if k < nums[l]: #k小于数组第一个元素
        nums.insert(l,k)
        return l,nums
    if k > nums[r]: #k大于数组最后一个元素
        nums.append(k)
        return r+1,nums
    while l<=r:
        mid=(l+r)//2
        if k>nums[mid]:
            l=mid+1
            if k<nums[l]:   #k不在数组内,位于相邻两个元素之间时
                nums.insert(l,k)
                return l,nums
        elif k<nums[mid]:
            r=mid
        elif k == nums[mid]:
            return mid,nums

nums=[1,5,6,8,9,13]
k=3
print(fun(nums,k))  #(1, [1, 3, 5, 6, 8, 9, 13])

复杂度分析

testeducative commented 2 years ago

思路

二分查找

代码


class Solution {
public:
    int searchInsert(vector<int>& nums, int target) {
        int left = 0, right = nums.size();
        while(right > left)
        {
            int mid = left + (right - left)/2;
            if(nums[mid] == target)
            {
                return mid;
            }
            else if(nums[mid] < target)
            {
                left = mid + 1;
            }
            else if(nums[mid] > target)
            {
                right = mid;
            }
        }
        return left;
    }
};
## 复杂度
时间O(logn) 空间O(1)
Flower-F commented 2 years ago

解题思路

二分

代码

/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number}
 */
var searchInsert = function (nums, target) {
    let left = 0, right = nums.length - 1;
    while (left <= right) {
        const mid = (left + right) >> 1;
        if (nums[mid] > target) {
            right = mid - 1;
        } else if (nums[mid] < target) {
            left = left + 1;
        } else {
            return mid;
        }
    }
    return left;
};

复杂度

时间:O(logN) 空间:O(1)

uniqlell commented 2 years ago

思路:二分

class Solution {
    public int searchInsert(int[] nums, int target) {
        int l = 0;
        int r = nums.length-1;
        while(l<=r){
            int mid = l+((r-l)>>1);
            if(nums[mid]>target){
                r = mid-1;
            }else if(nums[mid]<target){
                l = mid+1;
            }else{
                return mid;
            }
        }
        return r+1;
    }
}
Toms-BigData commented 2 years ago

【Day 27】35. 搜索插入位置

思路

很基础的二分法,考虑一下边界情况即可

golang代码

func searchInsert(nums []int, target int) int {
    if len(nums) == 0{
        return 0
    }
    if target <=nums[0]{
        return 0
    }
    if target == nums[len(nums)-1]{
        return len(nums)-1
    }
    if target > nums[len(nums)-1]{
        return len(nums)
    }
    l:=0
    r:=len(nums)
    for l<r{
        mid:= l+(r-l)/2
        if nums[mid]<target&&nums[mid+1]>=target{
            return mid+1
        }
        if nums[mid]<target{
            l = mid
        }else {
            r = mid
        }
    }
    return -1
}

复杂度

时间:O(logn) 空间:O(1)

Yachen-Guo commented 2 years ago

思路

因为出现了复杂度O(logn)的要求,所以采用二分法是最为合适的。具体代码实现是基础的二分法。

代码

class Solution {
    public int searchInsert(int[] nums, int target) {
        int left = 0, right = nums.length - 1;
        int mid;
        while(right >= left){
            mid = left + (right - left) / 2;
            if(nums[mid] < target) left = mid + 1;
            else if(nums[mid] > target) right = mid - 1;
            else{return mid;}
        }
        return left;
    }
}
haixiaolu commented 2 years ago

思路

二分查找

代码 / python

class Solution:
    def searchInsert(self, nums: List[int], target: int) -> int:
        left = 0
        right = len(nums) - 1

        while left <= right:
            mid = (left + right) // 2
            if target == nums[mid]:
                return mid
            elif target < nums[mid]:
                right = mid - 1
            else:
                left = mid + 1
        return left 

复杂分析

Moin-Jer commented 2 years ago

思路


二分

代码


class Solution {
    public int searchInsert(int[] nums, int target) {
        int l = 0, r = nums.length - 1;
        while (l <= r) {
            int mid = (l + r) >>> 1;
            if (nums[mid] == target) {
                return mid;
            }
            if (nums[mid] > target) {
                r = mid - 1;
            } else {
                l = mid + 1;
            }
        }
        return l;
    }
}

复杂度分析


biscuit279 commented 2 years ago

思路:

二分

class Solution:
    def searchInsert(self, nums: List[int], target: int) -> int:
        l,r = 0,len(nums)
        while l<r:
            mid = (l+r)//2
            if nums[mid] > target:
                r = mid
            elif nums[mid] == target:
                return mid
            else:
                l = mid + 1
        return l

时间复杂度:O(logn) 空间复杂度:O(1)

Myleswork commented 2 years ago

思路

二分

代码

class Solution {
public:
    int searchInsert(vector<int>& nums, int target) {
        int left = 0;
        int right = nums.size();
        while(left<right){
            int mid = (right-left)/2+left;
            if(nums[mid] >= target) right = mid;
            else left = mid+1;
        }
        return right;
    }
};

复杂度分析

时间复杂度:O(n)

空间复杂度:O(1)

JudyZhou95 commented 2 years ago

思路

二分搜索

代码

class Solution:
    def searchInsert(self, nums: List[int], target: int) -> int:
        if not nums or len(nums) == 0:
            return 0

        l, r = 0, len(nums)

        while l < r :
            mid = (l + r) // 2

            if target > nums[mid]:
                l = mid + 1
            else:
                r = mid 

        return l

复杂度

TC: O(logN) SC: O(1)

jiaqiliu37 commented 2 years ago
class Solution:
    def searchInsert(self, nums: List[int], target: int) -> int:
        left, right = 0, len(nums)-1

        while left <= right:
            mid = (left + right) // 2
            if nums[mid] == target:
                return mid
            elif nums[mid] < target:
                left = mid + 1
            else:
                right = mid -1

        return left

Time complexity O(logn) Space complexity O(n)

zol013 commented 2 years ago
class Solution:
    def searchInsert(self, nums: List[int], target: int) -> int:
        left, right = 0, len(nums) - 1

        while left <= right:
            mid = (right + left) // 2
            if nums[mid] == target:
                return mid
            elif nums[mid] > target:
                right = mid - 1
            else:
                left = mid + 1

        return left
chenyaohn commented 2 years ago

思路

二分查找

代码

public class Day27 {
    /**
     * 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。
     *
     * 请必须使用时间复杂度为 O(log n) 的算法。
     *
     */

    public int searchInsert(int[] nums, int target) {

        int left = 0;
        int right = nums.length-1;

        while (left<=right){//=条件处理target大于最后一位
            int mid = (left+right)/2;
            if(nums[mid]<target){//中间值小于于 目标值
                left = mid+1;
            }else if(nums[mid]==target){
                return mid;
            }else{//中间值大于 目标值
                right = mid-1;
            }
        }

        return left;
    }
}

复杂度分析

时间复杂度:O(logN)
空间复杂度:O(1)

junbuer commented 2 years ago

思路

代码

class Solution(object):
    def searchInsert(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """
        n = len(nums)
        l, r = 0 , n - 1
        while l <= r:
            mid = (r + l) >> 1
            if target == nums[mid]:
                return mid  
            elif target < nums[mid]:
                r = mid - 1
            else:
                l = mid + 1
        return l

复杂度分析

last-Battle commented 2 years ago

代码

C++ Code:


class Solution {
public:
    int searchInsert(vector<int>& nums, int target) {
        int start = 0, end = nums.size() - 1;
        int mid = 0;

        while (start <= end) {
            mid = start + (end - start) / 2;
            if (nums[mid] < target) {
                start = mid + 1;
            } else if (nums[mid] > target) {
                end = mid - 1;
            } else if (nums[mid] == target) {
                return mid;
            }
        }

        return start;
    }
};

复杂度分析

ywang525 commented 2 years ago

class Solution: def searchInsert(self, nums: List[int], target: int) -> int: l, r = 0, len(nums)-1 if nums[0] > target: return 0 if nums[-1] < target: return len(nums) while l <= r: mid = (l+r)//2 if nums[mid] == target: return mid elif nums[mid] > target: if mid > 0 and nums[mid-1] < target: return mid r = mid - 1 else: if mid < len(nums)-1 and nums[mid+1] > target: return mid + 1 l = mid + 1

stackvoid commented 2 years ago

思路

双指针

代码

class Solution {
    public int searchInsert(int[] nums, int target) {
        if (nums == null || nums.length == 0) return 0;
        int left = 0, right = nums.length - 1;
        if (target > nums[nums.length - 1]) return nums.length;
        if (target <= nums[0]) return 0;
        while (left < right) {
            int mid = (left + right) / 2;
            if (target > nums[mid]) {
                left = mid;
            } else if (target < nums[mid]) {
                right = mid;
            } else {
                return mid;
            }
            if ((left + 1) == right) break;

        }
        return right;
    }
}

复杂度分析

xiao-xiao-meng-xiang-jia commented 2 years ago

class Solution { public int searchInsert(int[] nums, int target) { int temp=0; for(int i=0; i<nums.length; i++){ if(nums[i]<target){ temp =i+1; }else if(nums[i] == target){ return i; }else{ break; } } return temp; } }

ginnydyy commented 2 years ago

Problem

https://leetcode.com/problems/search-insert-position/

Note

Solution

class Solution {
    public int searchInsert(int[] nums, int target) {
        if(nums == null || nums.length == 0){
            return 0;
        }

        int start = 0;
        int end = nums.length - 1;
        while(start + 1 < end){
            int mid = start + (end - start) / 2;
            if(target < nums[mid]){
                end = mid;
            }else if(target > nums[mid]){
                start = mid;
            }else{
                start = mid;
            }
        }

        if(target <= nums[start]){
            return start;
        }else if(target <= nums[end]){
            return end;
        }else{
            return end + 1;
        }
    }
}

Complexity

kite-fly6618 commented 2 years ago

思路

二分

代码

var searchInsert = function(nums, target) {
    let left = 0
    let right = nums.length
    while(left < right) {
        let mid = left + Math.floor((right - left) / 2)
        if (nums[mid] >= target) {
            right = mid
        }
        if (nums[mid] < target) {
            left = mid +1
        }
    }
    return left
}

复杂度

时间复杂度:O(logn)
空间复杂度:O(1)

feifan-bai commented 2 years ago

思路

  1. Two points, l, r to traverse the arrays

代码

class Solution:
    def searchInsert(self, nums: List[int], target: int) -> int:
        l, r = 0, len(nums)-1
        while l <= r:
            mid = (l+r) // 2
            if nums[mid] == target:
                return mid
            elif nums[mid] < target:
                l = mid+1
            else:
                r = mid-1
        return l

复杂度分析

GaoMinghao commented 2 years ago

思路

二分,需要小心r==l+1的情况

代码

class Solution {
    public int searchInsert(int[] nums, int target) {
        if(target > nums[nums.length-1]) return nums.length;
        if(target < nums[0]) return 0;
        int l = 0, r = nums.length - 1;
        int mid = (l + r) / 2;
        while (nums[mid]!= target) {
            if(r==l+1) {
                mid = mid+1;
                break;
            };
            if (nums[mid] > target) {
                r = mid;
            } else {
                l = mid;
            }
            mid = (l + r) / 2;
        }
        return mid;
    }
}

复杂度

时间复杂度 O(logN) 空间复杂度 O(1)

ray-hr commented 2 years ago

思路

class Solution:
    def searchInsert(self, nums: List[int], target: int) -> int:
        return len([ x for x in nums if x < target ])
freesan44 commented 2 years ago

思路

双指针

关键点

代码

Python3 Code:


class Solution:
    def searchInsert(self, nums: List[int], target: int) -> int:
        length = len(nums)
        if target > nums[-1]: return length
        left = 0
        right = length-1
        while left <= right:
            mid = (right+left)//2
            if nums[mid]>target:
                right = mid - 1
            elif nums[mid]<target:
                left = mid + 1
            else:
                return mid
        return left

复杂度分析

令 n 为数组长度。

wxj783428795 commented 2 years ago

思路

1.二分,没啥好说的

代码

/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number}
 */
var searchInsert = function (nums, target) {
    if (target > nums[nums.length - 1]) {
        return nums.length
    }
    if (target < nums[0]) {
        return 0
    }
    let start = 0;
    let end = nums.length - 1
    while (start < end) {
        let mid = Math.floor((start + end) / 2);
        let num = nums[mid];

        if (num === target) {
            return mid;
        } else if (num > target) {
            end = mid - 1
        } else {
            start = mid + 1
        }
    }
    return target <= nums[start] ? start : start + 1;
};

复杂度分析

复杂度分析不是很会,不一定对,如果有错,请指正。

KennethAlgol commented 2 years ago

思路

二分

class Solution {
    public int searchInsert(int[] nums, int target) {
        int left = 0, right = nums.length - 1;
        while(left <= right) {
            int mid = left + (right - left) / 2;
            if(nums[mid] == target) {
                return mid;
            } else if(nums[mid] < target) {
                left = mid + 1;
            } else if(nums[mid] > target){
                right = mid - 1;
            }
        }
        return left;
    }
}

复杂度分析

时间复杂度 O(logn) 空间复杂度 O(1)

ivangin commented 2 years ago

code

    public int searchInsert(int[] nums, int target) {
        if(nums==null || nums.length==0) return 0;
        int low = 0, high = nums.length-1;
        while(low <= high){
            int mid = (low + high)/2;
            if(nums[mid] == target){
                return mid;
            }
            else if(nums[mid] > target){
                high = mid - 1;
            }
            else{
                low = mid + 1 ;
            }

        }
        return low;
    }
shamworld commented 2 years ago
var searchInsert = function(nums, target) {
    let i = 0;
    let n = nums.length-1;
    let res = nums.length;
    while(i<=n){
        const mid = Math.floor((n+i)/2);
        if(nums[mid]>=target){
            res = mid;
            n=mid-1;
        }else{
            i=mid+1;
        }
    }
    return res;
};
ChenJingjing85 commented 2 years ago
class Solution:
    def searchInsert(self, nums: List[int], target: int) -> int:
        l = 0
        r = len(nums)-1
        while(l <= r):
            mid = l + ((r-l)>>1) #右移运算优先级小于加法,要加括号
            print(mid)
            if nums[mid] == target:
                return mid            
            elif nums[mid] > target:
                r = mid-1
            else:
                l = mid+1
        return l

time: log(n) space:1

zwx0641 commented 2 years ago

class Solution { public int searchInsert(int[] nums, int target) { int left = 0, right = nums.length - 1;

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

}

Macvh commented 2 years ago
class Solution(object):
    def searchInsert(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """
        low = 0
        high = len(nums) - 1

        while(low<=high):
            mid = (low+high)/2
            if(nums[mid]==target):
                return mid
            if(nums[mid]>target):
                high = mid - 1
            if(nums[mid]<target):
                low = mid + 1

        return low
lilililisa1998 commented 2 years ago

class Solution(object):     def searchInsert(self, nums, target):         """         :type nums: List[int]         :type target: int         :rtype: int         """         left=0         right=len(nums)-1         while left<=right:             mid=left+((right-left)//2)             if nums[mid] ==target:                 return mid             elif target>nums[mid]:                 left=mid+1             else:                 right=mid-1         return left

baddate commented 2 years ago

题目

https://leetcode-cn.com/problems/search-insert-position/

思路

二分法

代码

class Solution {
public:
    int searchInsert(vector<int>& nums, int target) {
        int left = 0, right = nums.size();
        while(left<right)
        {
            int mid = left+((right-left)/2);
            if(nums[mid]>target)
            {
                right = mid;
            }
            else if(nums[mid]<target)
            {
                left = mid+1;
            }
            else
            {
                return mid;
            }
        }
        return right;
    }
};

复杂度分析

L-SUI commented 2 years ago
var searchInsert = function(nums, target) {
    const n = nums.length;
    let left = 0, right = n - 1, ans = n;
    while (left <= right) {
        let mid = ((right - left) >> 1) + left;
        if (target <= nums[mid]) {
            ans = mid;
            right = mid - 1;
        } else {
            left = mid + 1;
        }
    }
    return ans;
};
Aobasyp commented 2 years ago

思路 二分

class Solution { public: int searchInsert(vector& nums, int target) { int l = 0, r = nums.size();

    while (l <= r) {
        int mid = l + (r - l)/2;
        if (nums[mid] == target) return mid;

        if (nums[mid] < target) l = mid + 1;
        else
            r = mid - 1;
    }

    return l;
}

};

时间复杂度: O(logn) 空间复杂度: O(1)

simbafl commented 2 years ago
class Solution:
    def searchInsert(self, nums: List[int], target: int) -> int:
        left = 0
        right = len(nums) - 1
        while left <= right:
            mid = (left + right) // 2
            if nums[mid] == target:
                return mid
            elif nums[mid] < target:
                left = mid+1
            elif nums[mid] > target:
                right = mid - 1
        return left
tangjy149 commented 2 years ago

思路

二分查找

代码

class Solution {
public:
    int searchInsert(vector<int>& nums, int target) {
        int i=0,j=nums.size()-1;
        while(i<=j){
           int mid=i+(j-i)/2;
           if(nums[mid]==target) return mid;
           if(nums[mid]>target){
               j=mid-1;
           }else{
               i=mid+1;
           }
        }
        return i;
    }
};

复杂度

时间复杂度:O(logn)
空间复杂度:O(1)

bluetomlee commented 2 years ago

思路

二分法

题解

// 二分法
class Solution {
public:
    int searchInsert(vector<int>& nums, int target) {
     int left = 0;
     int right = nums.size();
     while(left < right) {
         int middle = left + (right - left) / 2;
         if (nums[middle] >= target) {
             right = middle;
         } else {
             left = middle + 1;
         }
     }
     return left;
    }
};