题目

Given an unsorted integer array, find the first missing positive integer.

For example,

Given [1,2,0] return 3,

and [3,4,-1,1] return 2.

Your algorithm should run in O(n) time and uses constant space.

分析

题目给定一个无序序列,要求返回第一个丢失的正整数。

限制时间复杂度为O(n),空间消耗为常数。

这道题目其实很简单,只需要建立一个(len+1)的哈希hash,遍历序列,将(1~len)对应元素value放入hash[value],不符合要求的元素直接忽略。然后遍历哈希hash数组,寻找第一个对应位无值的元素即可。

但是该思路程序的空间复杂度为线性的,并不符合题目对空间消耗的要求,奇怪的是LeetCode OJ却是AC的。唉,苦苦思索常数空间消耗的算法,几次WA把我搞疯了都,不知道有没有常数空间的实现算法呢???

先给出OJ的AC算法吧~~~

AC代码

class Solution {
public:
int firstMissingPositive(vector<int>& nums) {
if (nums.empty())
return 1; //给定序列的长度
int len = nums.size();
vector<int> hash(len+1); for (int i = 0; i < len; i++)
{
if (nums[i] <= 0 || nums[i]>len)
continue;
else
hash[nums[i]] = 1;
} for (int i = 1; i < len+1; i++)
if (hash[i] != 1)
return i;
return len+1;
}
};

GitHub测试程序源码

LeetCode(41)First Missing Positive的更多相关文章

  1. (LeetCode 41)First Missing Positive

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

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

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

  3. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(41)-组织架构

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(41)-组织架构 本节开始我们要实现工作流,此工作流可以和之前的所有章节脱离关系,也可以紧密合并. 我们当 ...

  4. Windows Phone开发(41):漫谈关键帧动画之下篇

    原文:Windows Phone开发(41):漫谈关键帧动画之下篇 也许大家已经发现,其实不管什么类型的动画,使用方法基本是一样的,不知道大家总结出规律了没有?当你找到规律之后,你会发现真的可以举一反 ...

  5. SQL Server高可用——日志传送(4-1)——概论

    原文:SQL Server高可用--日志传送(4-1)--概论 本文作为学习总结,部分内容出自联机丛书及其他书籍 日志传送是什么? SQLServer 2012之前(2012出现了AlwaysOn), ...

  6. Qt 学习之路 2(41):model/view 架构

    Qt 学习之路 2(41):model/view 架构 豆子 2013年1月23日 Qt 学习之路 2 50条评论 有时,我们的系统需要显示大量数据,比如从数据库中读取数据,以自己的方式显示在自己的应 ...

  7. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  8. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  9. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

随机推荐

  1. 【OpenJ_Bailian - 4070 】全排列

    全排列 Descriptions: 对于数组[1, 2, 3],他们按照从小到大的全排列是 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1 现在给你一个正整数n,n小于8,输出 ...

  2. vs2013 安装 mvc5 的方法

    工具-->NuGet程序包管理器-->程序包管理器控制台 然后 PM>Install-Package Microsoft.AspNet.Mvc -Version 5.0.0

  3. vultr 购买vps

    基本安装转自:https://github.com/uxh/shadowsocks_bash/wiki/Vultr%E4%BD%BF%E7%94%A8%E6%95%99%E7%A8%8B 连接 Vul ...

  4. [BZOJ4043/CERC2014]Vocabulary

    Description 给你三个字符串,这些字符串有些单词模糊不可认了,用"?"来代表. 现在你可以用任意英文小写字母来代表它们.要求是使得给定的三个字符串中 所有的"? ...

  5. JEECMS9.3集成dubbo操作记录

    需求描述: 门户及其他应用系统需要查询JEECMS9.3中发布的栏目及数据,而其他系统都是基于dubbo开发的,因此想要将JEECMS9.3中集成dubbo并对外提供内容管理服务. 需求实现: 1.添 ...

  6. asp.net core连接sqlserver

    开发环境:win7,vs2017,sqlserver2014 vs上建立一个asp.net core web项目和一个.net core的类库项目DBA 简单起见,在DBA项目中就一个类SqlServ ...

  7. Android 线程池系列教程(3) 创建线程池

    Creating a Manager for Multiple Threads 上一课  下一课 1.This lesson teaches you to Define the Thread Pool ...

  8. IOS - PDF合并 - 转

    来自:http://www.cnblogs.com/tx8899/p/4082749.html #pragma mark - Merge PDF - (void)mergePDF { NSArray  ...

  9. ORACLE 如何查看存储过程的定义

    ORACLE 如何查看存储过程的定义   相关的数据字典 USER_SOURCE 用户的存储过程.函数的源代码字典 DBA_SOURCE 整个系统所有用户的存储过程.函数的源代码字典 ALL_SOUR ...

  10. Spring data jpa中Query和@Query分别返回map结果集

    引用: http://blog.csdn.net/yingxiake/article/details/51016234 http://blog.csdn.net/yingxiake/article/d ...