Leetcode练习(Python):数组类:第41题:给你一个未排序的整数数组,请你找出其中没有出现的最小的正整数。你的算法的时间复杂度应为O(n),并且只能使用常数级别的额外空间。


题目:给你一个未排序的整数数组,请你找出其中没有出现的最小的正整数。你的算法的时间复杂度应为O(n),并且只能使用常数级别的额外空间。
思路:第一个思路是创建一个锚点,这个锚点表示第一个正整数的出现的位置,然后再分情况来判断,结果程序无法通过所有的测试用例,第一个思路方法以后再实现,后来使用HashMap来说实现,十分方便。
程序1:HashMap
class Solution:
    def firstMissingPositive(self, nums: List[int]) -> int:
        length = len(nums)
        hashmap = [0] * length
        for x in nums:
            if x > 0 and x <= length:
                hashmap[x - 1] = x      
        for index in range(length):
            if hashmap[index] != index + 1:
                return index + 1
        return length + 1
程序2:类似专家系统(此程序有问题,待修正)
class Solution:
    def firstMissingPositive(self, nums: List[int]) -> int:
        nums.sort()
        length = len(nums)
        if length <= 0:
            return 1
        if length == 1:
            if nums[0] <= 0:
                return 1
            elif nums[0] == 1:
                return 2
            else:
                return 1
        #Find the first positive integer as anchor
        for index in range(length):
            if nums[index] > 0:
                anchor = index
                break
            else:
                anchor = length - 1
        #temp_index is the first anchor
        #anchor in the head
        if anchor == 0:
            if nums[anchor] == 1:
                while anchor < length:
                    #anchor += 1
                    if nums[anchor] - nums[anchor - 1] > 1:
                        return nums[anchor - 1] + 1
                        break
                    elif nums[anchor] - nums[anchor - 1] == 1 and anchor - 1 < length:
                        anchor += 1
                    elif nums[anchor] - nums[anchor - 1] == 0 and anchor - 1 < length:
                        anchor += 1
                    if anchor >= length - 1:
                        anchor = length - 1
                        return nums[anchor] + 1
                        break
            else:
                return 1
            
        #anchor in the tail
        elif anchor == length - 1:
            if nums[anchor] < 1:
                return 1
            elif nums[anchor] > 1:
                return 1
            else:
                return nums[anchor] + 1
        #anchor in the body
        else:
            while anchor < length:
                anchor += 1
                if nums[anchor] - nums[anchor - 1] > 1:
                    return nums[anchor - 1] + 1
                    break
                elif nums[anchor] - nums[anchor - 1] == 1 and anchor + 2 < length:
                    anchor += 1
                elif nums[anchor] - nums[anchor - 1] == 0 and anchor + 2 < length:
                    anchor += 1
                if anchor >= length - 1:
                    anchor = length - 1
                    return nums[anchor] +  1
                    break
                #anchor += 1
        #return anchor


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



猜您在找 Leetcode练习(Python):数组类:第128题:给定一个未排序的整数数组,找出最长连续序列的长度。 要求算法的时间复杂度为 O(n)。 41.给你一个未排序的整数数组,请你找出其中没有出现的最小的正整数。 Leetcode练习(Python):数组类:第34题:给定一个按照升序排列的整数数组 nums,和一个目标值 target。找出给定目标值在数组中的开始位置和结束位置。 你的算法时间复杂度必须是 O(log n) 级别。 如果数组中不存在目标值,返回 [-1, -1]。 如何对n个整数数进行排序,要求时间复杂度O(n),空间复杂度O(1) 给定一个长度为N的数组,找出出现次数大于n/2,n/3的数,要求时间复杂度O(n),空间复杂度O(1) Leetcode练习(Python):数组类:第53题:给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。进阶: 如果你已经实现复杂度为 O(n) 的解法,尝试使用更为精妙的分治法求解。 Leetcode练习(Python):数组类:第152题:给你一个整数数组 nums ,请你找出数组中乘积最大的连续子数组(该子数组中至少包含一个数字)。 Leetcode练习(Python):数组类:第209题:给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组。如果不存在符合条件的连续子数组,返回 0。 Leetcode练习(python):第414题:第三大的数:给定一个非空数组,返回此数组中第三大的数。如果不存在,则返回数组中最大的数。要求算法时间复杂度必须是O(n)。 在 O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序。
 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM