Question

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.

Solution 1 -- HashSet

Note the problem is to find first missing integer, ie, if input is [4,5,7,8], we should return 1.

Naive way is to traverse from 1 to length, find whether they are in original input set. Time complexity O(n), space cost O(n).

 public class Solution {
public int firstMissingPositive(int[] nums) {
int length = nums.length;
Set<Integer> set = new HashSet<Integer>();
for (int i = 0; i < length; i++)
set.add(nums[i]);
int result = 1;
for (int i = 1; i <= length; i++) {
if (!set.contains(i)) {
result = i;
break;
} else {
// Should increase here
// Consider input [1]
result++;
}
}
return result;
}
}

Solution 2

Key is to put i on position (i - 1). Time complexity O(n), space cost O(1).

 public class Solution {
public int firstMissingPositive(int[] nums) {
int length = nums.length;
// Put i on (i - 1) position
// Consider three conditions: 1. nums[i] <= 0 2. nums[i] > length 3. duplicates
for (int i = 0; i < length; i++) {
int target = nums[i];
if (target != i + 1) {
// nums[i] <= 0 nums[i] > length
if (target <= 0 || target > length)
continue;
// Duplicate
if (nums[target - 1] == target) {
nums[i] = 0;
continue;
}
// Swap target with nums[target - 1]
int tmp = nums[target - 1];
nums[target - 1] = target;
nums[i] = tmp;
// Still need to check nums[i] after swap;
i--;
}
} int result = length + 1;
// Check
for (int i = 0; i < length; i++) {
if (nums[i] != i + 1) {
result = i + 1;
break;
}
}
return result;
}
}

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. 【First Missing Positive】cpp

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

  3. 刷题41. First Missing Positive

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

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

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

  5. Leetcode First Missing Positive

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

  6. 【leetcode】First Missing Positive

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

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

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

  8. LeetCode - 41. First Missing Positive

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

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

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

随机推荐

  1. Visual Assist X在Windows 8.1下出现中文乱码的解决方法

    这主要是输入法造成的,我的输入法中有US.中文.搜狗输入法三个输入法:通过搜狗输入法管理器把“中文”去掉,或者通过语言首选项把“中文”去掉就不会在出现乱码. 这个办法的思路来自于http://www. ...

  2. Linux 使用yum工具

    Red Hat 发行版安装后无法使用yum,须注册方可使用,可以通过以下方式处理:cd /etc/yum.repos.d/备份目录下已经存在的repo文件,然后新建文件local.repo,具体脚本如 ...

  3. 【Python脚本】Eclipse IDE扩展PyDev插件安装

    作为一名Python的初学者,其实不用太在意IDE了,我觉得开始的时候用用自带的 IDLE 也挺好的. 还有 DreamPie 也挺好的.都是一些轻量级的IDE. 因为我正好安装有Eclipse,平时 ...

  4. 判断一个int 型整数 是否为回文数

    leetcode 上的题目 Determine whether an integer is a palindrome. Do this without extra space. 由于不能使用额外空间, ...

  5. python- 迭代器与生成器

    1.迭代器: 迭代器是访问集合元素的一种方式.迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器只能往前不会后退,不过这也没什么, 因为人们很少在迭代途中往后退.另外,迭代器的一 ...

  6. Android编译过程详解(二)

    通过上篇文章,我们分析了编译android时source build/envsetup.sh和lunch命令,在执行完上述两个命令后, 我们就可以进行编译android了. 1. make  执行ma ...

  7. js控制html5 audio的暂停、播放、停止

    <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <meta name ...

  8. linq和lamda表达式中添加时间判断时解决方案

    在工作中遇到个问题,在使用lamda查询数据的时候,需要添加一个时间判断, DateTime.AddDays(3) > e.ExpirationDate 例如:list = Context.Vo ...

  9. iOS 中的正则匹配(工具类方法)

    正则表达式 正则表达式是对字符串操作的一种逻辑公式, 用事先定义好的一些特定字符.及这些特定字符的组合, 组成一个"规则字符串", 这个"规则字符串"用来表达对 ...

  10. UIImage加载图片的区别和渲染模式

    前言 关于本地图片UIImage的加载问题,还是需要注意的.不同的加载处理方式,在效率和性能上还是有差异的. 今天,我们来讲讲UIImage的加载应该选择什么样的API来加载! 两种API 这两种AP ...