zoukankan      html  css  js  c++  java
  • LeetCode Easy: 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:

    Input: [1,3,5,6], 2
    Output: 1

    Example 3:

    Input: [1,3,5,6], 7
    Output: 4

    Example 1:

    Input: [1,3,5,6], 0
    Output: 0
    二、解题思路
    首先此题要分两种大情况:
    1、target in nums: 此种情况比较简单,遍历nums,找到target,返回索引即可;
    2、target not in nums:此种情况下,要分为三小种情况:
    (1)target比nums[0]小,返回0
    (2)target比num[len(nums)]大,返回len(nums)
    (3)target在nums[0]~nums[len(nums)-1]之间的话,就要比较用target与相邻的nums[i]和nums[i+1]比较
    三、代码
    #coding:utf-8
    import time
    def searchInsert1(nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """
        if target in nums:
            for i in range(len(nums)):
                if target == nums[i]:
                    print(i)
                    return i
        else:
            if target < nums[0]:
                print(0)
                return 0
            if target > nums[len(nums)-1]:
                print(len(nums))
                return len(nums)
    
            for j in range(len(nums)):
                if target > nums[j] and target < nums[j+1]:
                    print(j+1)
                    return j+1
    
    def searchInsert2(nums, target):
        first = 0
        last = len(nums) - 1
        while first < last:
            mid = (first + last + 1) // 2
            if nums[mid] == target:
                print(mid)
                return mid
            if nums[mid] < target:
                first = mid + 1
            else:
                last = mid - 1
        if nums[last] < target:
            print(last+1)
            return last + 1
        if target <= nums[last]:
            print(last)
            return last
        if target < nums[first]:
            print(first)
            return first
        print(first+1)
        return first + 1
    
    def searchInsert3(nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """
        low = 0
        high = len(nums) - 1
        while low <= high:
            mid = low + (high - low) / 2
            if nums[mid] < target:
                low = mid + 1
            elif nums[mid] > target:
                high = mid - 1
            else:
                return mid
        return low
    
    
    if __name__ == '__main__':
        a = [1,2,3,5,6,7,8]
        b = 4
        starttime = time.clock()
        searchInsert1(a,b)
        endtime = time.clock()
        print("the first run time is: %s ",(endtime - starttime))
        print("_______________")
        starttime2 = time.clock()
        searchInsert2(a, b)
        endtime2 = time.clock()
        print("the second run time is: %s",(endtime2 - starttime2))
        print("_______________")
        starttime3 = time.clock()
        #searchInsert3(a, b)
        endtime3 = time.clock()
        print("the third run time is: %s", (endtime3 - starttime3))
    考虑此问题,我并没有将数据结构考虑进来,所以并不完美,网上清一色的使用的是二分查找,把别人的代码贴出来,供学习,二分查找的速度比我写的快很多。










    既然无论如何时间都会过去,为什么不选择做些有意义的事情呢
  • 相关阅读:
    导出PDF乱码
    C/S模式下的打印方法
    填报表导出excel后不可写的单元格处于锁定状态
    多表批量导出txt及打压缩包下载
    客户端定时自动打印页面的例子
    套打中的自定义纸张设置
    linux客户端打印报表时操作系统的配置
    大数据量报表APPLET打印分页传输方案
    Python中类的定义及使用
    Solr7.7高级应用【MLT相似文档搜索、自动补全、自动纠错】
  • 原文地址:https://www.cnblogs.com/xiaodongsuibi/p/8643337.html
Copyright © 2011-2022 走看看