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) ...
随机推荐
- Vagrant 搭建开发环境实践
介绍 Development Environments Made Easy -官网标题 vagrant是一个命令行的虚拟机管理程序.用于简化搭建开发环境. vagrant使用ruby语言基于Chef ...
- IDA*、操作打表、并行处理-The Rotation Game HDU - 1667
万恶之源 优秀题解 用文字终究难以穷尽代码的思想 思路 每次操作都有八种选择,相当于一棵每次延申八个子节点的搜索树,故搜索应该是一种方法.而这题要求求最少步数,我们就可以想到可以试试迭代加深搜索(但其 ...
- 加法运算替代 牛客网 程序员面试金典 C++ Python
加法运算替代 牛客网 程序员面试金典 题目描述 请编写一个方法,实现整数的乘法.减法和除法运算(这里的除指整除).只允许使用加号. 给定两个正整数int a,int b,同时给定一个int type代 ...
- 翻转子串 牛客网 程序员面试金典 C++ Python
反转子串 牛客网 程序员面试金典 C++ Python 题目描述 假定我们都知道非常高效的算法来检查一个单词是否为其他字符串的子串.请将这个算法编写成一个函数,给定两个字符串s1和s2,请编写代码检查 ...
- H3C 三层交换基于IP限速
一.背景 目前百度爬虫爬取业务总是按照自己的性能进行抓取客户数据,从来不考虑客户端的网络承受能力,导致客户端网络带宽超出预算范围,因此在客户端方面针对百度的无限制抓取采取相应的策略. 二.解决方案: ...
- 攻防世界 WEB 高手进阶区 PHP2 Writeup
攻防世界 WEB 高手进阶区 PHP2 Writeup 题目介绍 题目考点 url 二次解码 index.phps 文件(第一次使用dirsearch可能扫不到,需要加到工具字典里) php 简单语法 ...
- Linux 服务器的基本性能及测试方法
1. 摘要 一个基于 Linux 操作系统的服务器运行的同时,也会表征出各种各样参数信息.通常来说运维人员.系统管理员会对这些数据会极为敏感,但是这些参数对于开发者来说也十分重要,尤其当程序非正常工作 ...
- elasticsearch在postman中创建复杂索引
body,所选类型为raw和JSON,写的代码为 { "settings":{ "number_of_shards":1, "number_of_re ...
- Part 23 to 26 Routing in Angular
Part 23 AngularJS routing tutorial In general, as the application becomes complex you will have more ...
- Java学习(十七)
Java多态的学习差不多有3个小时,老师还夹杂着一些编译器运用的知识. 这是多态的基本知识: 我们可以创建父类引用指向子类对象,这就是多态的一种.(这种也叫向下转型) Pet c=new Cat(); ...