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