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. java.utils.UUID类介绍

    1 UUID介绍 UUID (Universally Unique Identifier)缩写,即通用唯一识别码,也是被开源软件基金会 (Open Software Foundation, OSF) ...

  2. 【CSS】 布局之圣杯布局

    在看众多大神的css布局指南时,经常看到一个布局:圣杯布局(也有称为双飞翼布局的).今天我们也来剖析一下. 其实,对于众多css布局,我们只要明确理解了3种技术,那么基本上大多数布局都难不倒我们了: ...

  3. 1.7 js基础,字符串、数组小结

    一.arguments  实参参数的数组         实参[实际的值],形参[形式上的参数]         当参数个数不固定的时候使用.         示例: script> var g ...

  4. PHP多维数据排序(不区分大小字母)

    1. PHP中最普通的数组排序方法 sort(); 看个例子: <?php $test = array(); $test[] = 'ABCD'; $test[] = 'aaaa'; $test[ ...

  5. hosts文件配置参数介绍

    hosts文件配置参数介绍 1, ansible_ssh_host : 指定主机别名对应的真实 IP,如:100 ansible_ssh_host=192.168.1.100,随后连接该主机无须指定完 ...

  6. Spring ContextLoaderListener

    ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext的配置信息.因为它实现了ServletContextListener这个接口,在web ...

  7. alpine 上部署netcore 项目

    1 Alpine部署 注:以下教程是以Alpine v3.7.0系统部署:其他Linux系统部署也基本相同 1.1 .NET Core环境包下载 .net core下载地址:https://dotne ...

  8. 8、导航:Nav

    1.导航视图   angular2 中的是视图是显示在<router-outlet></router-outlet>里的同时他要依赖于 directives:[ ROUTER_ ...

  9. C#语言-07.文件操作

    a. 文件操作:适用于相对简单的数据保存 i. 读写文件的步骤: . 创建文件流 . 创建读写器 . 读写文件 . 关闭读写器 . 关闭文件流 ii. FileStream(文件流),它主要用于读写文 ...

  10. Redis - 事务操作

    Redis的事务基于四个命令: MULTI EXEC DISCARD WATCH 创建事务 Redis的事务从一个MULTI命令开始,MULTI总会命令返回"ok". 接着就可以开 ...