1. 程式人生 > >[Leetcode][python]First Missing Positive/缺失的第一個正數

[Leetcode][python]First Missing Positive/缺失的第一個正數

題目大意

給定一個未經排序的陣列,尋找第一個缺失的正整數
你的演算法應該滿足O(n)時間和常數空間複雜度

解題思路

儘可能地把陣列中不大於n(n為陣列長度)的正整數放置到下標+1與其數值相同的位置上

第一個下標+1與數值不同的數字,即為所求。

例如陣列nums = [3,4,-1,1],調整位置後的結果為:[1,-1,3,4]

除第二個數字外,其餘數字均滿足nums[i] = i + 1,因此返回2

程式碼

class Solution(object):
    def firstMissingPositive(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
n = len(nums) for i in range(n): # print nums[i], i + 1, nums[nums[i] - 1] while nums[i] > 0 and nums[i] <= n and nums[i] != i + 1 and nums[i] != nums[nums[i] - 1]: nums[nums[i] - 1], nums[i] = nums[i], nums[nums[i] - 1] # print nums
for i in range(n): if i + 1 != nums[i]: return i + 1 return n + 1

總結

tricky題目

相關推薦

[Leetcode][python]First Missing Positive/缺失第一正數

題目大意 給定一個未經排序的陣列,尋找第一個缺失的正整數 你的演算法應該滿足O(n)時間和常數空間複雜度 解題思路 儘可能地把陣列中不大於n(n為陣列長度)的正整數放置到下標+1與其數值相同的位置上 第一個下標+1與數值不同的數字,即為所求。

LeetCode | First Missing Positive第一缺失的正整數)

Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] return 3, and [3,4,-1,1] return 

[LeetCode] 41. First Missing Positive ☆☆☆☆☆(第一丟失的正數)

Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2,0]Output: 3Example 2: Input: [3,4,-1,1]Output: 2Exam

leetCode 41.First Missing Positive (第一丟失的正數) 解題思路和方法

First Missing Positive  Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] return 3, an

python leetcode 41. First Missing Positive

考察在陣列上的操作,nums[0]=1,nums[1]=2…按照這個順序在陣列上移動元素。然後在遍歷如果nums[index]!=index+1 那麼就找到了最小的丟失數。 這裡我們對陣列中數進行判斷,而不是下標,不然會死迴圈(例如[2,2,2,2]) class Solution:

[leetcode]41. First Missing Positive,C++/python實現,hard難度

題目 Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] return 3, and

[leetcode-41-First Missing Positive]

should pla gpo miss discuss com leet algo solution Given an unsorted integer array, find the first missing positive integer. For example,

[array] leetcode - 41. First Missing Positive - Hard

put 基本原理 理解 log 開始 blog ons i+1 right leetcode - 41. First Missing Positive - Hard descrition Given an unsorted integer array, find the f

LeetCode(41)-First Missing Positive

41-First Missing Positive Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2,0] Output: 3

LeetCode #41 First Missing Positive

(Week 2 演算法作業) 題目 分析 演算法1 演算法2 其他演算法 總結 題目 Given an unsorted integer array, find the smallest missing positive in

leetcode-41-First Missing Positive

Base on the idea: With the length of the array l, we can know that the result will be range from[1, l + 1]. E.g. Now we have array[1, 2, 3], the

Leetcode 41 First Missing Positive

Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2,0] Output: 3 Example 2: Input: [3,

Leetcode 41. First Missing Positive

array .com inpu get tor span n) nbsp sta 41. First Missing Positive 題目鏈接:https://leetcode.com/problems/first-missing-positive/ Descript

LeetCode-041 First Missing Positive

Description Given an unsorted integer array, find the smallest missing positive integer. Example Example 1: Input: [1,2,0] O

Leetcode 041 First Missing Positive(桶排序)

解題思路:桶排序的變種,每次把碰到把nums[i] 和 nums[nums[i]] 互換,保證 nums[i] = nums[nums[i]]。當然nums[i]小於零,或者是大於陣列的可以忽略。最後遍歷一遍陣列,第一個不匹配的位置即為答案。class Solution {

LeetCode 41. 缺失第一正數First Missing Positive

style 第一個 交換 ret etc 沒有 解題思路 ssi != 題目描述 給定一個未排序的整數數組,找出其中沒有出現的最小的正整數。 示例 1: 輸入: [1,2,0] 輸出: 3 示例 2: 輸入: [3,4,-1,1] 輸出: 2 示例 3: 輸入: [

[Swift]LeetCode41. 缺失第一正數 | First Missing Positive

Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2,0] Output: 3 Example 2: Input: [3,4,-1,1]

[LeetCode] First Missing Positive缺失正數

Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2,0] Output: 3 Example 2: Input: [3,4,-1,1]

First Missing Positive(在陣列中找到第一丟失的正整數)

題目原型: Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] return 3, and [3,4,-1,1] re

Leetcode First Missing Positive思路

First Missing Positive Given an unsorted integer array, find the first missing positive integer. For example, Given[1,2,0]return3, and[3