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

示例 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. 新闻实时分析系统Hive与HBase集成进行数据分析

    (一)Hive 概述 (二)Hive在Hadoop生态圈中的位置 (三)Hive 架构设计 (四)Hive 的优点及应用场景 (五)Hive 的下载和安装部署 1.Hive 下载 Apache版本的H ...

  2. DBEntry.Net 简介

    [尊重作者:本文转自:http://www.cnblogs.com/lephone/]   这是我设计的一个轻量级的 .Net ORM (Object Relational Mapping) 数据访问 ...

  3. 关于vue中的videoPlayer的src视频地址参数动态修改(网上一堆错误方法,被误导很久,自己找到了正确的方法,供大家借鉴)

    方法很简单:相信大家的问题应该是改变src的值吧,动态赋值这一步简单.this.playerOptions['sources'][0]['src'] 就是这一步解决提取src问题,主要部分用绿色框起来 ...

  4. python内存-fromkeys

    fromkeys 这个方法涉及到可变不可变类型,记录下测试代码 不可变类型 #可变类型-list x=["zx","zx2","zx3"] ...

  5. Singletone 析构函数调不到

    <设计模式>定义一个单例类,使用类的私有静态指针变量指向类的唯一实例,并用一个公有的静态方法获取该实例. 关键字:指向自己的静态指针私有,创建对象并赋值私有静态指针函数->公有, 构 ...

  6. 华为云北京四业务,访问北京一OBS桶,配置指南

    [摘要] 华为云跨数据中心,从北京四访问北京一的OBS桶里面的数据.免去数据迁移的麻烦 1      驱动力 跨region访问OBS桶里面的数据时.如果不走云连接,一个OBS桶域名对应的IP地址,是 ...

  7. echarts 堆叠柱状图 + 渐变柱状图

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. HDU-1698-----Just Hook

    In the game of DotA, Pudge's meat hook is actually the most horrible thing for most of the heroes. T ...

  9. HDU3870-Caught these thieves(最小割-&gt;偶图-&gt;最短路问题)

    A group of thieves is approaching a museum in the country of zjsxzy,now they are in city A,and the m ...

  10. SQL Server 2019 深度解读:微软数据平台的野望

    本文为笔者在InfoQ首发的原创文章,主要利用周末时间陆续写成,也算近期用心之作.现转载回自己的公众号,请大家多多指教. 11 月 4 日,微软正式发布了其新一代数据库产品 SQL Server 20 ...