Given an unsorted integer array, find the smallest missing positive integer.

Example 1:

Input: [1,2,0]
Output: 3

Example 2:

Input: [3,4,-1,1]
Output: 2

Example 3:

Input: [7,8,9,11,12]
Output: 1

Note:

Your algorithm should run in O(n) time and uses constant extra space.

solution: compare the element with index + 1, if not equal, swap them.

class Solution {
public int firstMissingPositive(int[] nums) {
//preprocess the string
//deal with duplicate elements--two pointers
//sorted to remove for(int i = ; i<nums.length; i++){
//check nums[i] != i+1
while(nums[i] > && nums[i] <= nums.length && nums[i] != nums[nums[i] -]){
//swap them
/*int temp = nums[i];
nums[i] = nums[nums[i] - 1];
nums[temp - 1] = temp;*/
int temp = nums[nums[i] - ];
if(temp == nums[i]) break; //check the duplicate elemnt
nums[nums[i] - ] = nums[i];
nums[i] = temp;
}
}
for(int i = ; i<nums.length; i++){
if(nums[i] <= || nums[i] != i+)
return i+;
}
return nums.length + ;
}
}

Easily, to solve this problem, we can use an extra space to solve this, like a hash set or array.

good reference: https://blog.csdn.net/SunnyYoona/article/details/42683405

41. First Missing Positive (sort) O(n) time的更多相关文章

  1. 刷题41. First Missing Positive

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

  2. LeetCode - 41. First Missing Positive

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

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

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

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

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

  5. LeetCode题解41.First Missing Positive

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

  6. 41. First Missing Positive(困难, 用到 counting sort 方法)

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

  7. 41. First Missing Positive

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

  8. 【一天一道LeetCode】#41. First Missing Positive

    一天一道LeetCode系列 (一)题目 Given an unsorted integer array, find the first missing positive integer. For e ...

  9. 【LeetCode】41. First Missing Positive (3 solutions)

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

随机推荐

  1. poj 2501 Average Speed

    Average Speed Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4842   Accepted: 2168 Des ...

  2. 事务的编写规范与Hibernate绑定session

    一.事务的编写规范 1.事务的基本概念: 数据库事务(Database Transaction) ,是指作为单个逻辑工作单元执行的一系列操作,要么完全地执行,要么完全地不执行. 事务处理可以确保除非事 ...

  3. [SpringBoot系列]--Spring Hibernate search 注解实现(未测试)

    1.maven项目pom.xml加入依赖 <dependency> <groupId>org.hibernate</groupId> <artifactId& ...

  4. 怎样以最快的速度导入mysql

    前一段时间团队举办数据库大赛,和我一组的小伙伴给我发了个链接,我觉得很有意思: https://dbahire.com/testing-the-fastest-way-to-import-a-tabl ...

  5. [转]oracle中查看用户权限

    本文转自:http://www.cnblogs.com/QDuck/archive/2010/08/11/1797225.html 1.查看所有用户:   select * from dba_user ...

  6. SSIS教程:创建简单的ETL包

    SSIS: Microsoft SQL Server Integration Services.是一个可用于生成高性能数据集成解决方案的平台,其中包括数据仓库的提取(Extract).转换(Trans ...

  7. .NET Unity IOC框架使用实例

    1.IOC简介 IOC(Inversion of Control), 控制反转 DI (Dependency Injection),依赖注入 IOC的基本概念是:不创建对象,但是描述创建它们的方式.在 ...

  8. 我所理解的js闭包

    举个例子: function f1(){ var n=; function f2(){ alert(n); } } 上面代码中,f2()可以读取f1()中的局部变量n的值,但是f1()不能反过来读取f ...

  9. 2013 Warm up 3 -- Skill --- dp

    题意:求n位数字,满足非递减的个数. dp[ i ] [ j ] = sum( dp[i -1] [ k ] );  k =>( j , 9); #include<iostream> ...

  10. hdu 1561 树形背包 选k个最大价值

    http://blog.csdn.net/dellaserss/article/details/8799730 这题其实和上一题思路是一样的,一个0节点作为根节点,通过剩余量来遍历子树. #inclu ...