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. JVM垃圾回收原理

    原文地址:http://chenchendefeng.iteye.com/blog/455883 一.相关概念 基本回收算法 1. 引用计数(Reference Counting) 比较古老的回收算法 ...

  2. Android如何添加多张引导页

    摘要:项目需要添加多张引导页,所以在网上搜集了一些资料并整理好. Step1 添加一个GuideActivity. 其实这个引导页无非就是一个Activity,里面有一个ViewPager而已.多张图 ...

  3. 自定义RadioGrop,支持添加包裹着的RadioButton

    控件类: package com.chinaCEB.cebView; import android.annotation.TargetApi; import android.content.Conte ...

  4. 编程高手解读什么是NodeJs?

    首先在搞清楚什么NodeJs之前,我们先来聊聊JavaScript,只要做过开发的人都应该知道JavaScript是目前最为流行的前端(客户端)脚 本语言,JavaScript在Web项目中的使用率可 ...

  5. Android EditText禁止换行键(回车键enter)

    在EditText所在的xml文件中,设置android:singleLine="true", 则可以禁止掉虚拟键盘: maxlength为该EditText的最大输入长度: &l ...

  6. c++中读取文件最快的方法

    https://www.byvoid.com/blog/fast-readfile 可以看看了.

  7. CodeForces839B[思维] Codeforces Round #428 (Div. 2)

    #include <bits/stdc++.h> using namespace std; int n, k; ; ], cnt[]; void solve() { int t; cnt[ ...

  8. @inerface的11条规范写法

    总结一些interface声明时的规范,相关宏的介绍,定义方法时有用的修饰符,编写注释的规范,最终写出一个合格的头文件. 1.读写权限 1.1实例变量的@public,@protected,@priv ...

  9. 写js时常见错误

    最近几天写js时出现好多相同的错误,确实应该记下来了 ReferenceError: invalid assignment left-hand side 判断相等时把"=="写成& ...

  10. eclipse快捷键补全

    Eclipse中 补全快捷键 默认Alt+/ 但是每个人习惯有所不同 我需要来修改自己熟悉的快捷键 windows->preferences->General->keys将Conte ...