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. WifiDog and OpenWrt

    $Id$ 2   3 OpenWRT specific README 4 ======================= 5   6 So, you want to run wifidog on on ...

  2. nodejs学习笔记之网络编程

    了解一下OSI七层模型   OSI层 功能 TCP/IP协议 应用层 文件传输,电子邮件,文件服务,虚拟终端  TFTP,HTTP,SNMP,FTP,SMTP,DNS,Telnet 表示层 数据格式化 ...

  3. pod setup 报CocoaPods was not able to update the `master` repo 错误解决办法

    先删除全局的缓存: $ sudo rm -fr ~/Library/Caches/CocoaPods/ $ sudo rm -fr ~/.cocoapods/repos/master/ 还不行的话就把 ...

  4. ZOJ Goldbach 2013年长沙赛区网络赛

    迟到了一天的AC.... 思路: 先把单个素数 或着 两个素数能组成的情况预处理一下,然后对于给出的 n,拿第三个素数去和两个素数的情况匹配,最后要注意去重. 详情见代码. 因为手残少敲了一个 els ...

  5. css-文字

    <!DOCTYPE html>CSS2-文字 <style>div{ /*文字*/ font-size:120px; /*文字大小*/ font-family:Arial,'方 ...

  6. 关于android的SQLiteDatabase和Cursor的一些疑问

    android数据库操作的基础有三个类:SQLiteOpenHelper,SQLiteDatabase和Cursor.其中,SQLiteOpenHelper会建立一个数据库连接,它虽然可以调用多次ge ...

  7. js 时间YYYY-MM-DD转换为YYYY/MM/DD 自定义函数格式

    <script type="text/javascript">         window.onload = function () {             va ...

  8. ios 获取屏幕的属性

    屏幕尺寸     CGRect screen = [UIscreen mainScreen].bounds 状态栏尺寸  CGRect rect = [[UIApplication sharedApp ...

  9. (原)vs2013编译boost1.60库

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5394236.html 参考网址: http://www.cnblogs.com/chuncn/arch ...

  10. MySql存储引擎介绍

    MySQL5.5以后默认使用InnoDB存储引擎,其中InnoDB和BDB提供事务安全表,其它存储引擎都是非事务安全表.若要修改默认引擎,可以修改配置文件中的default-storage-engin ...