[LeetCode Python3] 41. First Missing Positive 一步步優化
Solution 1:Time O(nlogn); Space O(1)
# Time O(nlogn); Space O(1)
class Solution:
def firstMissingPositive(self, nums: List[int]) -> int:
nums.sort()
index, size = 0, len(nums)
while index < size and nums[index] < 1:
index += 1
target = 1
while index < size and nums[index] == target:
while index < size and nums[index] == target:
index += 1
target += 1
return target
Solution 2:Time O(n); Space O(n)
# Time O(n); Space O(n)
class Solution:
def firstMissingPositive(self, nums: List[int]) -> int:
bucket = set()
target = 1
for num in nums:
if num == target:
target += 1
while target in bucket:
bucket.remove(target)
target += 1
elif num > target:
bucket.add(num)
return target
Solution 3:Time O(n); Space O(1)
解法3來自:https://leetcode.com/problems/first-missing-positive/discuss/872224/JavaPython-Supper-Clean-and-Concise-Time-O(N)-Space-O(1)
class Solution:
def firstMissingPositive(self, nums: List[int]) -> int:
n = len(nums)
INF = n + 1
for i in range(n):
if nums[i] <= 0:
nums[i] = INF # marks zero or negative numbers as infinitive positive numbers
for i in range(n):
x = abs(nums[i]) - 1 # use index start with zero
if x < n:
nums[x] = -abs(nums[x]) # mark `x` as visited by marking `nums[x]` as negative
for i in range(n):
if nums[i] > 0: # if nums[i] is positive -> number (i+1) is not visited.
return i + 1
return n + 1
相關文章
- Leetcode First Missing PositiveLeetCode
- LeetCode:Missing Number And First Missing PositiveLeetCode
- Leetcode 41 First Missing PositiveLeetCode
- Leetcode-First Missing PositiveLeetCode
- First Missing Positive leetcode javaLeetCodeJava
- First Missing Positive【hard】
- 41. First Missing Positive(找到陣列中未出現的最小正整數)陣列
- Leetcode-Missing RangesLeetCode
- LeetCode 41. 缺失的第一個正數LeetCode
- 一步步搭建 VuePress 及優化【自動化】Vue優化
- 【Leetcode】163. Missing RangesLeetCode
- 一步步搭建 VuePress 及優化【外掛系列】Vue優化
- 一步步搭建 VuePress 及優化【初始化到釋出】Vue優化
- [LeetCode] 2028. Find Missing ObservationsLeetCode
- [LeetCode] Find First and Last Position of Element in SortedLeetCodeAST
- 41. The Security Namespacenamespace
- leetcode:41. 缺失的第一個正數(困難,陣列)LeetCode陣列
- Number.POSITIVE_INFINITY
- Leetcode 34 Find First and Last Position of Element in Sorted ArrayLeetCodeAST
- SAP Fiori Positive User Experience
- 【優化】ALL_ROWS模式和FIRST_ROWS模式的適用場景優化模式
- LeetCode刷題(javascript,python3)LeetCodeJavaScriptPython
- 使用合適的設計模式一步步優化前端程式碼設計模式優化前端
- 14-1 雜湊表基礎 / Leetcode first uniq charLeetCode
- FIRST_ROWS優化模式訪問遠端表可能導致錯誤結果(二)優化模式
- FIRST_ROWS優化模式訪問遠端表可能導致錯誤結果(一)優化模式
- Oracle的優化器:RBO/CBO,RULE/CHOOSE/FIRST_ROWS/ALL_ROWS 名詞解釋Oracle優化
- 【知識點】深度優先搜尋 Depth First Search
- 10小時到10分鐘,一步步優化巨量關鍵詞的匹配優化
- 41.部署LNMP平臺原始碼LNMP原始碼
- leetcode【210】【Depth-first Search】Course Schedule II【c++版本】LeetCodeC++
- OCP(11g)-----> oracle First In First Out (FIFO)/Last In First OutOracleAST
- API優先(API-first)是一個壞主意! - stilkovAPI
- Missing Subsequence Sum
- ntldr is missing怎麼解決? ntldr is missing怎麼回事?
- 從零到千萬使用者,我是如何一步步優化MySQL資料庫的?優化MySql資料庫
- 前端效能優化(JS/CSS優化,SEO優化)前端優化JSCSS
- jQuery first()jQuery