zoukankan      html  css  js  c++  java
  • LeetCode-35. Search Insert Position | 搜索插入位置

    题目

    LeetCode
    LeetCode-cn

    Given a sorted array of distinct integers 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.

    Example 1:
    Input: nums = [1,3,5,6], target = 5
    Output: 2
    
    Example 2:
    Input: nums = [1,3,5,6], target = 2
    Output: 1
    
    Example 3:
    Input: nums = [1,3,5,6], target = 7
    Output: 4
    
    Example 4:
    Input: nums = [1,3,5,6], target = 0
    Output: 0
    
    Example 5:
    Input: nums = [1], target = 0
    Output: 0
     
    Constraints:
    1 <= nums.length <= 104
    -104 <= nums[i] <= 104
    nums contains distinct values sorted in ascending order.
    -104 <= target <= 104
    

    题解

    题目为简单难度。

    解法一:暴力求解法

    使用for循环遍历nums数组,从下标0开始按个与目标值target进行对比,如果nums[i]>=target,说明目标值在数组所有元素之前,直接返回i即可;另一种情况就是目标值在数组所有元素之后,这时返回nums数组长度len(nums)即可。

    //Go
    func searchInsert(nums []int, target int) int {
        for i := 0;i <len(nums);i++ {
            if nums[i] >= target {
                return i
            }
        }
        
        return len(nums)
    }
    

    执行结果:

    leetcode:
    Runtime: 4 ms, faster than 88.29% of Go online submissions for Search Insert Position.
    Memory Usage: 3.1 MB, less than 100.00% of Go online submissions for Search Insert Position.
    
    leetcode-cn:
    执行用时:4 ms, 在所有 Go 提交中击败了90.15%的用户
    内存消耗:2.9 MB, 在所有 Go 提交中击败了100.00%的用户
    

    解法二:二分查找法

    //Go
    func searchInsert(nums []int, target int) int {
        low := 0
        high := len(nums) - 1
        for low <= high {
            // 下方写法为了防止数据溢出,如果先加在除以2 加完的值可能会大于INT_MAX,造成溢出 
            mid := low + (high - low) / 2
            guess := nums[mid]
            if guess == target {
                return mid //找到了,返回下标
            }
            if guess > target {
                high = mid - 1
            } else {
                low = mid +1
            }
        }
    
        return low //没找到
    }
    

    执行结果:

    leetcode-cn:
    执行用时:4 ms, 在所有 Go 提交中击败了90.15%的用户
    内存消耗:3 MB, 在所有 Go 提交中击败了56.60%的用户
    
    leetcode:
    Runtime: 8 ms, faster than 8.78% of Go online submissions for Search Insert Position.
    Memory Usage: 3.3 MB, less than 6.08% of Go online submissions for Search Insert Position.
    

    解法三:golang-sort.SearchInts

    解题时不推荐。

    //Go
    func searchInsert(nums []int, target int) int {
        return sort.SearchInts(nums, target)
    }
    

    执行结果:

    leetcode:
    Runtime: 4 ms, faster than 88.29% of Go online submissions for Search Insert Position.
    Memory Usage: 3.1 MB, less than 100.00% of Go online submissions for Search Insert Position.
    
    leetcode-cn:
    执行用时:4 ms, 在所有 Go 提交中击败了90.15%的用户
    内存消耗:3 MB, 在所有 Go 提交中击败了56.60%的用户
    
             
    版权声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明。
        
    特此声明:所有评论和私信都会在第一时间回复。也欢迎园子里和园子外的大大们指正错误,共同进步。或者直接私信我 (^∀^)
        
    声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是作者坚持原创和持续写作的最大动力!

    您的资助是我最大的动力!
    金额随意,欢迎来赏!

    如果,您认为阅读这篇博客让您有些收获,不妨点击一下右下角的推荐按钮。
    如果,您希望更容易地发现我的新博客,不妨点击一下绿色通道的关注我

    如果,想给予我更多的鼓励,求打

    本博客的所有打赏均将用于博主女朋友的化妆品购买以及养肥计划O(∩_∩)O。我是【~不会飞的章鱼~】!

    联系或打赏博主【~不会飞的章鱼~】!https://www.cnblogs.com/OctoptusLian/

  • 相关阅读:
    Codevs 2296 仪仗队 2008年省队选拔赛山东
    Codevs 1535 封锁阳光大学
    Codevs 1069 关押罪犯 2010年NOIP全国联赛提高组
    Codevs 1218 疫情控制 2012年NOIP全国联赛提高组
    Codevs 1684 垃圾陷阱
    洛谷 P1108 低价购买
    Vijos P1325桐桐的糖果计划
    Codevs 3289 花匠 2013年NOIP全国联赛提高组
    Codevs 2611 观光旅游(floyed最小环)
    C语言基础之彩色版C语言(内含linux)
  • 原文地址:https://www.cnblogs.com/OctoptusLian/p/14401071.html
Copyright © 2011-2022 走看看