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.

思路:n个数,可能的最大正整数是n,所以可以将这n个数作为哈希值。但是这样要有O(n)的额外空间。

解决方法是复用A[], 首先将A[i]中的负数剔出(标记为n+1);第二次遍历时,将<=n的正整数交换到下标为n-1的位置。

class Solution {
public:
int firstMissingPositive(vector<int>& nums) {
int size = nums.size();
int tmp,i;
for(i =; i < size; i++){
if(nums[i]<=) nums[i]=size+;
} i = ;
while(i < size){
if(nums[i]<=size && nums[i]!=i+){
if(nums[nums[i]-]==nums[i]){//repeat digit occurs
nums[i]=size+;
i++;
}
else{
tmp=nums[nums[i]-];
nums[nums[i]-]=nums[i];
nums[i]=tmp;
}
}
else{
i++;
}
} for(int i = ; i < size; i++){
if(nums[i]!=i+){
if(i>) return nums[i-]+;
else return ;
}
}
return size+;
}
};

改进: 我们可以不用tmp进行交换,省去O(1)的空间,也减少赋值的次数。当遍历到一个整数cur的时候,将A[cur]变成相反数,这样A[i]<0的i位置表示i出现过了,还保证了A[i]原来的值没被破坏(可以通过abs(A[i])获得)。

class Solution {
public:
int firstMissingPositive(int A[], int n) {
int i;
for (i = ; i < n; i++)
if(A[i]<=) A[i] = n+;
for (i = ; i < n; i++) {
if(abs(A[i]) <= n ){ //需要abs,因为在for循环中,我们将值为[1,n]的元素改成了负数
int cur = abs(A[i])-;
A[cur] = -abs(A[cur]);
}
}
for (i = ; i < n; i++) {
if (A[i] > ) return i+;
}
return n+;
}
};

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

  1. LeetCode - 41. First Missing Positive

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

  2. [Leetcode][Python]41: First Missing Positive

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 41: First Missing Positivehttps://oj.le ...

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

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

  4. LeetCode题解41.First Missing Positive

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

  5. 刷题41. First Missing Positive

    一.题目说明 题目是41. First Missing Positive,求一个未排序队列中缺失的最小正整数.时间复杂度要求是O(n).难度是Hard,确实难. 二.我的解答 不考虑时间复杂度,首先对 ...

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

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

  7. 41. First Missing Positive (JAVA)

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

  8. leetcode 41 First Missing Positive ---java

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

  9. 41. First Missing Positive

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

随机推荐

  1. Beta阶段第2周/共2周 Scrum立会报告+燃尽图 12

    作业要求[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2411] 版本控制:https://git.coding.net/liuyy08 ...

  2. maven 笔记:maven Could not create the Java Virtual Machine

    1.安装好maven,在cmd中运行mvn –v,报错:“maven  Could not create the Java Virtual Machine” 2.分析:这是跟jvm有关,在cmd中运行 ...

  3. tensorflow中 tf.reduce_mean函数

    tf.reduce_mean 函数用于计算张量tensor沿着指定的数轴(tensor的某一维度)上的的平均值,主要用作降维或者计算tensor(图像)的平均值. reduce_mean(input_ ...

  4. pyalgotrade入门

    入门代码解析: from pyalgotrade import strategyfrom pyalgotrade.barfeed import yahoofeed #继承自BacktestingStr ...

  5. Java第三次作业--面向对象基础(封装)

    Deadline: 2017-4-6 23:00 一.学习要点 认真看书并查阅相关资料,掌握以下内容: 掌握简单类的设计 掌握利用对象引用建立类与类之间的联系 掌握this关键字 掌握static关键 ...

  6. 51Nod 1049:最大子段和(dp)

    1049 最大子段和  基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 N个整数组成的序列a[1],a[2],a[3],-,a[n],求该序列如a[i]+ ...

  7. Android 开发有哪些新技术出现?

    这里记录一下在知乎回答的<Android 开发有哪些新技术出现?>.知乎链接在这里. 原问题如下: Android 开发有哪些新技术出现?可以从UI设计或者一些核心的算法之类的说起 这是我 ...

  8. bzoj2721樱花——质因数分解

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2721 要推式子! 发现x和y一定都比 n! 大.不妨设 x = n!+k: 则1/x + 1 ...

  9. MyEclipse部署项目到Tomcat上,但是classes文件夹下没有编译项目

    在MyEclipse中把项目部署到Tomcat上,但是Tomcat下的classes文件夹下没有编译项目解决方法:1-直接在点击菜单栏的Project--clean,对项目进行clean2-查看菜单栏 ...

  10. 'scalar deleting destructor' 和 'vector deleting destructor'的区别

    在用到delete的时候,我们往往会针对类对象与类对象数组做不同删除,在这背后编译器是如何做的? #include<iostream> using namespace std; class ...