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缺失的第一个正数的更多相关文章

  1. LeetCode:缺失的第一个正数【41】

    LeetCode:缺失的第一个正数[41] 题目描述 给定一个未排序的整数数组,找出其中没有出现的最小的正整数. 示例 1: 输入: [1,2,0] 输出: 3示例 2: 输入: [3,4,-1,1] ...

  2. Leetcode 41.缺失的第一个正数

    缺失的第一个正数 给定一个未排序的整数数组,找出其中没有出现的最小的正整数. 示例 1: 输入: [1,2,0] 输出: 3 示例 2: 输入: [3,4,-1,1] 输出: 2 示例 3: 输入: ...

  3. Java实现 LeetCode 41 缺失的第一个正数

    41. 缺失的第一个正数 给定一个未排序的整数数组,找出其中没有出现的最小的正整数. 示例 1: 输入: [1,2,0] 输出: 3 示例 2: 输入: [3,4,-1,1] 输出: 2 示例 3: ...

  4. LeetCode(41):缺失的第一个正数

    Hard! 题目描述: 给定一个未排序的整数数组,找出其中没有出现的最小的正整数. 示例 1: 输入: [1,2,0] 输出: 3 示例 2: 输入: [3,4,-1,1] 输出: 2 示例 3: 输 ...

  5. leetcode之缺失的第一个正数

    给定一个未排序的整数数组,找出其中没有出现的最小的正整数. 示例 1: 输入: [1,2,0]输出: 3示例 2: 输入: [3,4,-1,1]输出: 2示例 3: 输入: [7,8,9,11,12] ...

  6. [Leetcode] first missing positve 缺失的第一个正数

    Given an unsorted integer array, find the first missing positive integer. For example,Given[1,2,0]re ...

  7. 【LeetCode】缺失的第一个正数【原地HashMap】

    给定一个未排序的整数数组,找出其中没有出现的最小的正整数. 示例 1: 输入: [1,2,0] 输出: 3 示例 2: 输入: [3,4,-1,1] 输出: 2 示例 3: 输入: [7,8,9,11 ...

  8. LeetCode 41. 缺失的第一个正数(First Missing Positive)

    题目描述 给定一个未排序的整数数组,找出其中没有出现的最小的正整数. 示例 1: 输入: [1,2,0] 输出: 3 示例 2: 输入: [3,4,-1,1] 输出: 2 示例 3: 输入: [7,8 ...

  9. leetcode 41缺失的第一个正数

    time O(n) space O(1) class Solution { public: int firstMissingPositive(vector<int>& nums) ...

随机推荐

  1. 解决boa网页操作出现502 Bad Gateway The CGI was not CGI/1.1 compliant的一种可能

    最近在把一套网页操作的接口从原来Android5.0上移植到Android7.0上. 客户端连接验证的时候主页显示异常 502 Bad Gateway The CGI was not CGI/1.1 ...

  2. poj 2960 S-Nim (SG)

    题意: K个数,s1...sk. m个状态,对于某一个状态,有L堆石子,每人每次取的石子个数只能是s1...sk的一个,且只能在一堆中取. 输出m个状态是先手胜还是先手败,先手胜输出W,否则输出L. ...

  3. Go语言核心36讲(Go语言进阶技术十二)--学习笔记

    18 | if语句.for语句和switch语句 现在,让我们暂时走下神坛,回归民间.我今天要讲的if语句.for语句和switch语句都属于 Go 语言的基本流程控制语句.它们的语法看起来很朴素,但 ...

  4. 30分钟通过Kong实现.NET网关

    什么是Kong Openrestry是一个基于Nginx与Lua的高性能平台,内部有大量的Lua库.其中ngx_lua_moudule使开发人员能使用Lua脚本调用Nginx模块.Kong是一个Ope ...

  5. netty系列之:搭建客户端使用http1.1的方式连接http2服务器

    目录 简介 使用http1.1的方式处理http2 处理TLS连接 处理h2c消息 发送消息 总结 简介 对于http2协议来说,它的底层跟http1.1是完全不同的,但是为了兼容http1.1协议, ...

  6. Piakchu之RCE漏洞

    一.Ping(远程系统命令执行) 首先正常输入一个ip,查看页面的返回值.发现有乱码,但是能看出执行了ping命令. 查看源代码,可以看到只是对操作系统进行了判断,而对输入内容是否为ip地址并没有判断 ...

  7. systemd-nspawn以及container的学习

    container的分类 目前container可以分为两大类,一类是Privileged container,一类是Unprivileged container. Privileged contai ...

  8. Jmeter 正则表达式提取Response Headers,Response Body里的值

    实践过程中遇到需要提取Response Headers,Response Body里的值 一.获取Response Body的值,这里采用json提取器形式 1.Response Body返回值,如下 ...

  9. Intellij IDEA 配置Junit

    导包: 1.Hamcrest Core 包:    https://mvnrepository.com/artifact/org.hamcrest/hamcrest-core/1.3 2.Junit包 ...

  10. Mac下Shell脚本使用学习笔记(一)

    参考文献 Shell 教程 MAC常用终端命令行 Mac下Shell脚本使用 1.使用终端创建test.sh: (1)进入指定文件夹路径(命令示例:cd Desktop/面向对象程序设计): (2)创 ...