题目

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. nginx https反向代理tomcat

    Context体现在server.xml中的Host里的<Context>元素,它由Context接口定义.每个<Context>元素代表了运行在虚拟主机上的单个Web应用. ...

  2. bnu oj 13288 Bi-shoe and Phi-shoe

    题目链接: http://www.bnuoj.com/contest/problem_show.php?pid=13288 题目大意: 给出一个n,然后给出n个幸运数([1,m]中不能被m整除的数的数 ...

  3. Java标识符的习惯命名规范

    1 常量标识符:全部用大写字母和下划线表示.如SALES_MAX 2 类名或接口名:标识符用大写字母开头.如CreditCard 3 变量名和方法名:以小写字母开头,单词之间不要有分隔符,第二 及后面 ...

  4. matlab实现算术编解码 分类: 图像处理 2014-06-01 23:01 357人阅读 评论(0) 收藏

    利用Matlab实现算术编解码过程,程序如下: clc,clear all; symbol=['abc']; pr=[0.4 0.4 0.2]; %各字符出现的概率 temp=[0.0 0.4 0.8 ...

  5. Android 线程池系列教程(4) 启动线程池中的线程和中止池中线程

    Running Code on a Thread Pool Thread 上一课   下一课 1.This lesson teaches you to Run a Runnable on a Thre ...

  6. 【先定一个小目标】Asp.net Core 在IIS上的托管运行

    1.安装 .NET Core Framework 下载.net core地址:官网地址 2.Install IIS 在控制面板->程序与功能->Internet Infomation Se ...

  7. input 全选 jquery封装方法

    HTML代码 <table class="table table-striped"> <thead> <tr> <th><in ...

  8. OpenGl之旅-—初识opengl

    昨天学习了如何使用codeblocks来编译运行一个opengl的项目.在创建一个新的opengl项目时他默认已经写了一个示例,今天我们就上面的例子进行下代码的剖析,以此来敲开opengl的神秘大门. ...

  9. idea下关联spark源码环境(转)

    0.环境: java 1.8 scala 2.11.8 maven 3.5.0 idea 2017 spark 2.2.0 1完成以下配置 java环境变量 scala环境变量 maven setti ...

  10. mac 下使用gcc 编译c函数

    mac 终端其实和window 的cmd类似,由于mac 的os x 采用了unix 系统,所以,各种类似UNIX下的命令都有用.最近在看computer science ,用到了命令行. 下面是一个 ...