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. visual studio cl -d1reportSingleClassLayout查看内存f分布

    C:\Users\Administrator\Desktop\cppsrc>cl -d1reportSingleClassLayoutTeacher virtual.cpp 用于 x86 的 M ...

  2. DFS:POJ1190-生日蛋糕(基础搜索)

    生日蛋糕 Time Limit: 1000MS Memory Limit: 10000K 描述 7月17日是Mr.W的生日,ACM-THU为此要制作一个体积为Nπ的M层生日蛋糕,每层都是一个圆柱体. ...

  3. 动态规划:完全背包问题-HDU1114-Piggy-Bank

    解题心得: 1.这是一个完全背包问题的变形,题目要求是求在规定的重量下求价值最小,所以需要将d[0]=0关键的初始化 2.当不可能出现最小的价值时,d的状态并没有被改变,说明并没有放进去一个硬币. 题 ...

  4. 后缀数组的使用心得——POJ2774 最长连续公共子串

    对于这道题,将两个字符串直接合并成为一个字符串,分别记录连个字符串结束的位置. 首先,应用黑暗圣典的模板,我们可以顺利得到height,rank,sa三个数组. 之后直接扫描1-n所有的位置,选出来一 ...

  5. (Winform)控件中添加GIF图片以及运用双缓冲使其不闪烁以及背景是gif时使控件(如panel)变透明

    Image img = Image.FromFile(@"C:\Users\joeymary\Desktop\3.gif"); pictureBox1.Image =img.Clo ...

  6. Maya建模命令

    Surface-Loft(放样)在两条曲线中间生成曲面Section Radius 改变圆环的圆环半径Edit Mesh- Merge 点连结挤压 keep face together(整体挤压),若 ...

  7. datagrid的右键菜单

    1. 2.右键菜单,主要是用onRowContextMenu:function(e,index,row){}方法来实现 onRowContextMenu:function(e,index,row){ ...

  8. MySQL安装教程&Navicat安装

    一.下载MySQL http://jingyan.baidu.com/article/e3c78d64412ae83c4c85f5fd.html 首先打开MySQL官网,找到Downloads标签,点 ...

  9. 使用 SceneLoader 类在 XNA 中显示载入屏幕(十)

    平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...

  10. [python][django学习篇][14]markdown 代码高亮

    1 修改detail视图函数,渲染文件的时候,增加codehight拓展 post.body = markdown.markdown(post.body, extensions=[ 'markdown ...