给定一个未排序的整数数组,找出其中没有出现的最小的正整数。

示例 1:

输入: [1,2,0]
输出: 3
示例 2:

输入: [3,4,-1,1]
输出: 2
示例 3:

输入: [7,8,9,11,12]
输出: 1
说明:

你的算法的时间复杂度应为O(n),并且只能使用常数级别的空间。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/first-missing-positive
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

 class Solution {
public:
int firstMissingPositive(vector<int>& nums) {
//这是看了解答后的代码 满足题意 但速度没提高太多
int size = nums.size(); int *numsHash;
int size_hash = size +; //创建一个哈希表 初始化为0
numsHash = new int[size_hash]();
//遍历nums 对于小于数组长度的元素在哈希表中打个标记
//对于比数组长度大的数据可以忽略,因为第一个缺失的正数没这么大
for (int i = ; i < size; ++i)
{
if (nums[i] > && nums[i] < size_hash)
{
numsHash[nums[i]] = ;
}
} //遍历哈希表 找出第一个没打标记的
int *end_pos = numsHash + size_hash;
for (int *p = numsHash + ; p != end_pos; ++p)
{
if (*p == )
{
return p - numsHash;
}
} return size_hash;
} int firstMissingPositiveNew(vector<int>& nums) { //这是我自己想的答案,不符合题目O(n)要求,但排名也挺高
//排序 然后找出第一个大于0的数字
//顺序查找下去,直到发现本元数比上一个元素大1就说明找到了
sort(nums.begin(), nums.end());
       //从1开始找出第一个不在数组里面的正数 慢
//for (int i = 1; i <= INT_MAX; ++i)
//{
// if (!binary_search(nums.begin(), nums.end(), i))
// {
// return i;
// }
//} auto it = upper_bound(nums.begin(), nums.end(), ); if (it == nums.end() || *it != ) return ; for (++it; it != nums.end(); ++it)
{
if ((*it - *(it - )) > )
{
return *(it - ) + ;
}
} return *(it - ) + ;
}
};
执行结果:

通过
显示详情
执行用时 :4 ms, 在所有 cpp 提交中击败了83.16%的用户
内存消耗 :8.7 MB, 在所有 cpp 提交中击败了73.72%的用户

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】缺失的第一个正数【原地HashMap】

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

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

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

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

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

  7. LeetCode缺失的第一个正数

    LeetCode 缺失的第一个正数 题目描述 给你一个未排序的整数数组 nums,请你找出其中没有出现的最小的正整数. 进阶:你可以实现时间复杂度为 O(n)并且只使用常数级别额外空间的解决方案吗? ...

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

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

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

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

随机推荐

  1. 3 JAVA的基本变量类型

    1. 数字 整数型   类型 字节数  范围 int  4 -2^31~ 2^31-1 short 2 -2^15~ 2^15 -1 long 8 -2^63 ~ 2^63 -1 byte 1 -2^ ...

  2. 封装一个适用于vue的 jsonp

    import originJsonp from 'jsonp' export default function jsonp(url, data, option) { return new Promis ...

  3. css圆,背景,img填满等样式

    background 属性 属性值 描述 background-color 单词颜色表示法.rgb.十六进制 设置元素的背景颜色 background-image url('http://www.aa ...

  4. Find命令、文件名后缀、Linux和Windows互传文件 使用介绍

    第2周第5次课(3月30日) 课程内容: 2.23/2.24/2.25 find命令2.26 文件名后缀 2.27 Linux和Windows互传文件 find命令 文件查找: 1.which(一般用 ...

  5. JSON.parse() 报错和一些解决方法

    js 报错 Unexpected end of JSON input,Unexpected token u in JSON at position 0 JSON 通常用于与服务端交换数据. 在接收服务 ...

  6. Dictionary的遍历

    Dictionary<string, int> list = new Dictionary<string, int>(); list.Add("d", 1) ...

  7. 深入 .NET Core 基础 - 2:共享框架

    深入 .NET Core 基础 - 2:共享框架 原文地址:https://natemcmaster.com/blog/2018/08/29/netcore-primitives-2/ 共享框架从 . ...

  8. tensorflow学习笔记——AlexNet

    1,AlexNet网络的创新点 AlexNet将LeNet的思想发扬光大,把CNN的基本原理应用到了很深很宽的网络中.AlexNet主要使用到的新技术点如下: (1)成功使用ReLU作为CNN的激活函 ...

  9. Kubernetes增强型调度器Volcano算法分析

    [摘要] Volcano 是基于 Kubernetes 的批处理系统,源自于华为云开源出来的.Volcano 方便 AI.大数据.基因.渲染等诸多行业通用计算框架接入,提供高性能任务调度引擎,高性能异 ...

  10. kali linux中文乱码解决

    命令中输入 LANG=en_US.UTF-8 apt-get install ttf-wqy-microhei xfonts-wqy gnome-tweak-tool