Leetcode 35.搜索插入位置 By Python

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

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

示例 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

思路

  • python的list是有index()方法的,如果目标值在数组中就很简单
  • 如果没有的话就从头线性扫描一遍,当然更好的做法是二分找位置

代码

class Solution:
    def searchInsert(self, nums: List[int], target: int) -> int:
        try:
            index_value = nums.index(target)
            return index_value
        except:
            length = len(nums)
            for i in range(length):
                if nums[i] > target and i > 0:
                    return i
                if nums[i] > target and i == 0:
                    return 0
                if nums[i] < target and i == length-1:
                    return length

# 二分法
class Solution:
    def searchInsert(self, nums: List[int], target: int) -> int:
        length = len(nums)
        if length == 0:
            return 0
        left = 0
        right = length

        while left < right:
            mid = (left + right) // 2
            if nums[mid] < target:     # 此时已经排除mid位置是答案的可能性
                left = mid + 1
            else:
                right = mid
        return left  

精选题解里有教二分法模板,说的很棒,强烈推荐



注明

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/search-insert-position

原文地址:https://www.cnblogs.com/MartinLwx/p/LeetcodeByPython.html

时间: 2024-08-04 12:30:23

Leetcode 35.搜索插入位置 By Python的相关文章

[leetcode] 35. 搜索插入位置(Java)(二分)

35. 搜索插入位置 二分,太简单,没啥好说的 class Solution { public int searchInsert(int[] nums, int target) { if (nums.length == 0) return 0; int i = 0, j = nums.length; int mid = (i + j) / 2; while (i < j) { if (nums[mid] == target) { return mid; } else if (nums[mid]

35. 搜索插入位置

35. 搜索插入位置 https://leetcode-cn.com/problems/search-insert-position/description/ package com.test; public class Lesson035 { public static void main(String[] args) { int[] nums = {1,3,5,6}; int target = 5; int in = searchInsert(nums, target); System.ou

LeetCode刷题--35. 搜索插入位置(简单)

题目描述 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元素. 示例 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 思路:二分法 如果该题目暴力解决的话需要 O(n)的时间复杂度,但是如果二分的话则可以降低到 O(logn)的时间

leetCode 第35题,搜索插入位置

题目概述 题目:力扣:35.搜索插入位置 难易:简单 内容: 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元素. 示例 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 来源:力扣(LeetCode) 链接:https://

【leetcode】Clone Graph(python)

类似于二叉树的三种遍历,我们可以基于遍历的模板做很多额外的事情,图的两种遍历,深度和广度模板同样也可以做很多额外的事情,这里举例利用深度优先遍历的模板来进行复制,深度优先中,我们先访问第一个结点,接着访问第一个邻接点,再访问邻节点的邻节点.... class Solution: # @param node, a undirected graph node # @return a undirected graph node def cloneGraph(self, node): if None =

[leetcode]Merge k Sorted Lists @ Python

原题地址:https://oj.leetcode.com/problems/merge-k-sorted-lists/ 题意:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解题思路:归并k个已经排好序的链表.使用堆这一数据结构,首先将每条链表的头节点进入堆中,然后将最小的弹出,并将最小的节点这条链表的下一个节点入堆,依次类推,最终形成的链表就是归

[leetcode]Search for a Range @ Python

原题地址:https://oj.leetcode.com/problems/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

[leetcode]Search a 2D Matrix @ Python

原题地址:https://oj.leetcode.com/problems/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 o

[leetcode]Evaluate Reverse Polish Notation @ Python

原题地址:https://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题意: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples