LeetCode 41. First Missing Positive--Python 解法--数学题-找到不存在的最小正整数-O(1)空间复杂度
题目地址:First Missing Positive - LeetCode
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]
Output: 2
Example 3:
Input: [7,8,9,11,12]
Output: 1
Note:
Your algorithm should run in O(n) time and uses constant extra space.
这道题目跟这道很像:LeetCode 31. Next Permutation-- Python 解法–数学题–比当前数大的最小的数
但这道题目难度明显更大,因为没有明确数组大小,而且有负数。
首先,时间复杂度至少为O(N),因为要遍历数组一遍,空间复杂度可大可小,但O(N)的复杂度是可以接受的。
首先确定数组长度,最小缺失的正整数最大为N+1
遍历数组,使用bitmap确认存这个数是否存在过
Python解法如下:
class Solution:def firstMissingPositive(self, nums) -> int:length = len(nums)l = [0]*(length+2)for i in nums:if 0 < i < 1 + length:l[i] = 1for i in range(1, length+3):if l[i] == 0:return i
此解法时间复杂度为O(n),空间复杂度为O(n)。
想要空间复杂度降为O(1)是可以做到的,但不一定会更快,因为CPU缓存的原因,到处交换数组不一定快:
class Solution:def firstMissingPositive(self, nums) -> int:end = len(nums)i = 0while i != end:val = nums[i]if val == i+1:i += 1elif val > end or val <= 0 or nums[val-1] == val:end -= 1nums[i], nums[end] = nums[end], nums[i]else:nums[i], nums[val-1] = nums[val-1], nums[i]return i+1
LeetCode 41. First Missing Positive--Python 解法--数学题-找到不存在的最小正整数-O(1)空间复杂度相关推荐
- 【排序+难题】LeetCode 41. First Missing Positive
LeetCode 41. First Missing Positive 本博客转载自:[1]http://www.cnblogs.com/grandyang/p/4395963.html [2]htt ...
- [LeetCode]41.First Missing Positive
[题目] Given an unsorted integer array, find the first missing positive integer. For example, Given [1 ...
- leetcode 41. First Missing Positive 1
题目要求 Given an unsorted integer array, find the first missing positive integer.For example, Given [1, ...
- leetCode 41.First Missing Positive (第一个丢失的正数) 解题思路和方法
First Missing Positive Given an unsorted integer array, find the first missing positive integer. For ...
- leetcode 41. First Missing Positive
https://www.cnblogs.com/grandyang/p/4395963.html https://www.jianshu.com/p/cf82ce91dc3d 错误解法1: [1,1] ...
- [leetcode]41. First Missing Positive
题目地址 https://leetcode.com/problems/first-missing-positive/ 题目大意 一个整数数组,里面数字是无序的,在O(n)的时间复杂度,O(1)的空间复 ...
- LeetCode题解41.First Missing Positive
41. First Missing Positive Given an unsorted integer array, find the first missing positive integer. ...
- LeetCode 264. Ugly Number II--C++,Python解法
题目地址:Ugly Number II - LeetCode Write a program to find the n-th ugly number. Ugly numbers are positi ...
- LeetCode 20. Valid Parentheses--笔试题--Python解法
题目地址:Valid Parentheses - LeetCode Given a string containing just the characters '(', ')', '{', '}', ...
最新文章
- GitHub开源:一键生成前后端代码神器
- 计算机和网络知识,计算机和网络技术基础知识
- JavaScript最新手机号码、电话号码正则表达式
- devops_您的DevOps阅读心愿单的10本书
- Linux-环境变量的设置和查看
- IOT设备的7大安全问题
- Java中常用的设计模式【模板模式】
- chrome谷歌浏览器历史版本
- 绝地求生主播御用手机雷达分屏
- WordPress直接调用头像地址
- html多个div横向排列居中,多个div垂直居中横向排列的示例分析
- JPA中@Enumerated注解
- 本地计算机如何使用代理服务器,自动设置代理ip
- matlab中的clc命令和clear命令
- 回眸2020,展望2021
- 负载阻抗、感抗、容抗
- dlib php,图片人脸检测——Dlib版(四)
- ISCC2022-MISC-降维打击
- beego的安装和升级
- 【Bioconductor系列】如何用Bioconductor对基因组注释
热门文章
- RDKit:化合物骨架分析
- python中的下划线_Python中的下划线详解
- MER: 基于ITS区域marker扩增真菌群落的准确性
- 2019微生物组—宏基因组分析专题培训第三期
- 哈佛牙学院博士后:教你口腔保健基本功之刷牙篇
- 叶际微生物定殖模型研究进展
- SourceTracker—微生物来源分析
- 丰度决定了细菌在复杂群落中的功能作用
- Python使用numpy包编写自定义函数计算平均绝对误差(MAE、Mean Absolute Error)、评估回归模型和时间序列模型、解读MAE
- python使用matplotlib可视化线图(line plot)、并自定义线条的粗细(线条的宽度、 line width in Matplotlib)