LeetCode缺失的第一个正数
LeetCode 缺失的第一个正数
题目描述
给你一个未排序的整数数组 nums,请你找出其中没有出现的最小的正整数。
进阶:你可以实现时间复杂度为 O(n)并且只使用常数级别额外空间的解决方案吗?
示例 1:
输入:nums = [1,2,0]
输出:3
示例 2:
输入:nums = [3,4,-1,1]
输出:2
示例 3:
输入:nums = [7,8,9,11,12]
输出:1
Java 解法
/**
* @author zhkai
* @date 2021年3月29日13:51:23
*/
public class FirstMissingPositive {
/**
* 给你一个未排序的整数数组 nums,请你找出其中没有出现的最小的正整数
*
* @param nums 未排序的整数数组 nums
* @return 没有出现的最小的正整数
*/
public static int firstMissingPositive(int[] nums) {
int numsLen = nums.length;
if (numsLen == 0) {
return 1;
}
int[] res = new int[numsLen + 1];
int resLen = res.length;
for (int x : nums) {
if (x > 0 && x < resLen) {
res[x] = x;
}
}
for (int i = 1; i < resLen; i++) {
if (i != res[i]) {
return i;
}
}
return resLen;
}
/**
* 给你一个未排序的整数数组 nums,请你找出其中没有出现的最小的正整数
*
* @param nums 未排序的整数数组 nums
* @return 没有出现的最小的正整数
*/
public static int firstMissingPositiveTwo(int[] nums) {
int numsLen = nums.length;
if (numsLen == 0) {
return 1;
}
for (int i = 0; i < numsLen; i++) {
while (nums[i] > 0 && nums[i] < numsLen + 1 && nums[i] != i + 1 && nums[i] != nums[nums[i] - 1]) {
swap(nums, i, nums[i] - 1);
}
}
for (int i = 0; i < numsLen; i++) {
if (nums[i] != i + 1) {
return i + 1;
}
}
return numsLen + 1;
}
/**
* 交换数组元素位置
*
* @param nums 未排序的整数数组 nums
* @param i 需交换元素数组index
* @param j 与需交换元素进行交换的数组index
*/
public static void swap(int[] nums, int i, int j) {
if (i != j) {
nums[j] ^= nums[j];
nums[j] ^= nums[i];
nums[i] ^= nums[j];
}
}
}
Java 解法效率对比
输入:nums = {1, 3, 6, 7, 9};
方法一:2708900ns
方法二:15400ns
Python 解法
from typing import List
def first_missing_positive(nums: List[int]) -> int:
"""
给你一个未排序的整数数组 nums,请你找出其中没有出现的最小的正整数
:param nums: 未排序的整数数组
:return: 没有出现的最小的正整数
"""
n = len(nums)
res = [0 for i in range(n + 1)]
for x in nums:
if 0 < x < len(res):
res[x] = x
for i in range(len(res)):
if res[i] != i:
return i
return len(res)
def first_missing_positive_two(nums: List[int]) -> int:
"""
给你一个未排序的整数数组 nums,请你找出其中没有出现的最小的正整数
:param nums: 未排序的整数数组
:return: 没有出现的最小的正整数
"""
n = len(nums)
for i in range(n):
if 0 < nums[i] < n + 1 and nums[1] != i + \
1 and nums[i] != nums[nums[i] - 1]:
swap(nums, i, nums[i] - 1)
for i in range(n):
if nums[i] != i + 1:
return i + 1
return n + 1
def swap(nums: List[int], i: int, j: int):
if i != j:
nums[i] ^= nums[j]
nums[j] ^= nums[i]
nums[i] ^= nums[j]
Python 解法效率对比
输入:nums = {1, 3, 6, 7, 9};
方法一:13900ns
方法二:17100ns
LeetCode缺失的第一个正数的更多相关文章
- LeetCode:缺失的第一个正数【41】
LeetCode:缺失的第一个正数[41] 题目描述 给定一个未排序的整数数组,找出其中没有出现的最小的正整数. 示例 1: 输入: [1,2,0] 输出: 3示例 2: 输入: [3,4,-1,1] ...
- Leetcode 41.缺失的第一个正数
缺失的第一个正数 给定一个未排序的整数数组,找出其中没有出现的最小的正整数. 示例 1: 输入: [1,2,0] 输出: 3 示例 2: 输入: [3,4,-1,1] 输出: 2 示例 3: 输入: ...
- Java实现 LeetCode 41 缺失的第一个正数
41. 缺失的第一个正数 给定一个未排序的整数数组,找出其中没有出现的最小的正整数. 示例 1: 输入: [1,2,0] 输出: 3 示例 2: 输入: [3,4,-1,1] 输出: 2 示例 3: ...
- LeetCode(41):缺失的第一个正数
Hard! 题目描述: 给定一个未排序的整数数组,找出其中没有出现的最小的正整数. 示例 1: 输入: [1,2,0] 输出: 3 示例 2: 输入: [3,4,-1,1] 输出: 2 示例 3: 输 ...
- leetcode之缺失的第一个正数
给定一个未排序的整数数组,找出其中没有出现的最小的正整数. 示例 1: 输入: [1,2,0]输出: 3示例 2: 输入: [3,4,-1,1]输出: 2示例 3: 输入: [7,8,9,11,12] ...
- [Leetcode] first missing positve 缺失的第一个正数
Given an unsorted integer array, find the first missing positive integer. For example,Given[1,2,0]re ...
- 【LeetCode】缺失的第一个正数【原地HashMap】
给定一个未排序的整数数组,找出其中没有出现的最小的正整数. 示例 1: 输入: [1,2,0] 输出: 3 示例 2: 输入: [3,4,-1,1] 输出: 2 示例 3: 输入: [7,8,9,11 ...
- LeetCode 41. 缺失的第一个正数(First Missing Positive)
题目描述 给定一个未排序的整数数组,找出其中没有出现的最小的正整数. 示例 1: 输入: [1,2,0] 输出: 3 示例 2: 输入: [3,4,-1,1] 输出: 2 示例 3: 输入: [7,8 ...
- leetcode 41缺失的第一个正数
time O(n) space O(1) class Solution { public: int firstMissingPositive(vector<int>& nums) ...
随机推荐
- 你知道怎么从jar包里获取一个文件的内容吗
目录 背景 报错的代码 原先的写法 编写测试类 找原因 最终代码 背景 项目里需要获取一个excle文件,然后对其里的内容进行修改,这个文件在jar包里,怎么尝试都读取不成功,但是觉得肯定可以做到,因 ...
- surrounded-regions leetcode C++
Given a 2D board containing'X'and'O', capture all regions surrounded by'X'. A region is captured by ...
- sum-root-to-leaf-numbers leetcode C++
Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number. ...
- Luogu P1297 [国家集训队]单选错位 | 概率与期望
题目链接 题解: 单独考虑每一道题目对答案的贡献. 设$g_i$表示gx在第$i$道题目的答案是否正确(1表示正确,0表示不正确),则$P(g_i=1)$表示gx在第$i$道题目的答案正确的概率. 我 ...
- Bzoj P2054 疯狂的馒头 | 并查集
题目链接 思路:因为每次染色都会将某些馒头的颜色彻底更改,所以每个馒头的最终的颜色其实是由最后一次染色决定的,那么我们只考虑最后一次染色即可.对此,我们可以从后往前倒着染色,当目前的染色区间中存在白色 ...
- hdu 2189 来生一起走(DP)
题意: 有N个志愿者.指挥部需要将他们分成若干组,但要求每个组的人数必须为素数.问不同的方案总共有多少.(N个志愿者无差别,即每个组的惟一标识是:人数) 思路: 假设N个人可分为K组,将这K组的人数从 ...
- poj 1330 Nearest Common Ancestors (最简单的LCA)
题意: 给出一棵树的结构. 给出两个点X和Y,求它俩的LCA. 思路: 只需求两个点的LCA,用了两种方法,一种离线tarjan,一种直接搞. 看代码. 代码: 方法一:直接搞. int const ...
- prometheus(6)之常用服务监控
监控常用服务 1.tomcat 2.redis 3.mysql 4.nginx 5.mongodb prometheus监控tomcat tomcat_exporter地址 https://githu ...
- css三大特性 & 选择器的权重
层叠性 层叠性是指当一个标签被设置了多个重复的样式的时候会发生冲突,一个属性会覆盖另外一个属性. 覆盖性原则: 层叠性主要遵循的原则是就近原则,在不考虑优先级的情况下,在多个样式中最终生效的样式是离标 ...
- pip 常用命令小结
pip 常用命令小结 pip这个工具我们经常会用到,毕竟python 是一门以第三方库庞大而著名的编程语言,所以我们总会用pip 安装一些依赖库,当然这只是pip 最常用的一个命令,下面就来介绍一下 ...