https://oj.leetcode.com/problems/first-missing-positive/

给一列数,找出缺失的第一个正数。要求时间复杂度 O(n)

第一步遍历一遍,找出最大的数和最小的数。

第二步建立一个vector,以 max+1 为size。

第三部遍历一遍,存储每个存在的数到相应的下标那里。

第四部遍历一遍,寻找数组中第一个计数是0的数。

class Solution {
public:
int firstMissingPositive(int A[], int n) {
int min = ;
int max = ;
//find the smallest and the biggest
for(int i = ;i<n;i++)
{
max = A[i]>max?A[i]:max;
min = min>A[i]?A[i]:min;
}
if(max == )
return ;
if(min == )
return ; vector<int> map;
map.resize(max+);
for(int i = ;i<n;i++)
{
if(A[i]>)
map[A[i]] = ;
}
for(int i =;i<max;i++)
if(map[i]==)
return i;
return max+;
}
};

LeetCode OJ-- First Missing Positive的更多相关文章

  1. [array] leetcode - 41. First Missing Positive - Hard

    leetcode - 41. First Missing Positive - Hard descrition Given an unsorted integer array, find the fi ...

  2. 【leetcode】 First Missing Positive

    [LeetCode]First Missing Positive Given an unsorted integer array, find the first missing positive in ...

  3. leetcode 41 First Missing Positive ---java

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

  4. [LeetCode] 41. First Missing Positive 首个缺失的正数

    Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...

  5. 【leetcode】First Missing Positive

    First Missing Positive Given an unsorted integer array, find the first missing positive integer. For ...

  6. 【leetcode】First Missing Positive(hard) ☆

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

  7. LeetCode - 41. First Missing Positive

    41. First Missing Positive Problem's Link ---------------------------------------------------------- ...

  8. LeetCode题解-----First Missing Positive

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

  9. Java for LeetCode 041 First Missing Positive

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

  10. Java [Leetcode 41]First Missing Positive

    题目描述: Given an unsorted integer array, find the first missing positive integer. For example,Given [1 ...

随机推荐

  1. String java问题随笔

    1.字符串加密 设计思想: 每个字符都能够转化为整数型,也能将整数型转化为字符类型,这样我们在加密时候由于向后推3个,所以可以将字符转换为整形,然后加3,之后在将运算完的变量转化为字符后输出,就可以实 ...

  2. 图学java基础篇之IO

    java io体系 如图可以看出,java的io按照包来划分的话可以分为三大块:io.nio.aio,但是从使用角度来看,这三块其实揉杂在一起的,下边我们先来概述下这三块: io:主要包含字符流和字节 ...

  3. 3224: Tyvj 1728 普通平衡树(finger tree)

    3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 19122  Solved: 8359[Submit][St ...

  4. Nodejs-内置核心模块&npm包管理工具

    1.核心模块的意义 如果只是在服务器运行JavaScript代码,其实意义不大(浏览器就可以解决)因为无法实现功能(读写文件,访问网络) Node的用处在于本身还提供了一系列的功能模块,用于与操作系统 ...

  5. Beats、Filebea入门

    1. Filebeat配置简介 2. Filebeat收集nginx日志 3. packetbeat简介与演示

  6. Fragment 和 Activity 之间通信

    在 Activity 中获取 Fragment 实例: FragmentManager 提供了一个类似于 findViewById 的方法,专门用于从布局文件中获取 Fragment 实例: //通过 ...

  7. 新博客 http://kunyashaw.com/

    感谢博客园. 请关注我的新博客: http://kunyashaw.com/

  8. 【Permutation Sequence】cpp

    题目: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...

  9. TextBox的值随dropdownlist值而变化

    转自:http://bytes.com/topic/asp-net/answers/443065-textbox-value-change-select-other-item-dropdownlist ...

  10. idea热部署设置(复制)

    提出问题 IntelliJ IDEA工具如何设置热部署??? 解决问题 我的IDEA的版本是:IntelliJ IDEA 14.0.2 第一步:打开tomcat配置 这里写图片描述 第二步: 这里写图 ...