题目

给出一个无序的整数数组,找出其中没有出现的最小正整数。

样例

如果给出 [1,2,0], return 3 如果给出 [3,4,-1,1], return 2

挑战

只允许时间复杂度O(n)的算法,并且只能使用常数级别的空间。

解题 

感觉好像好像很复杂,通过率21%也是比较低的了。

找了一个很不好的方法

步骤:

1.先找出数组中正整数的最小值,Min

2.若Min>1 显然最小的不在数组中的正整数就是1

3.这里的都是最小值Min == 1的情况

对于这个情况,只需要判断  对最小的i, Min + i 是否在数组中的正整数部分,这里需要定义一个ArrayList存放正整数部分的数,方便判断Min+i是否在ArrayList中。最小的i对于的Min+i就是答案。

这里时间复杂度和空间复杂度都是O(N)

public class Solution {
/**
* @param A: an array of integers
* @return: an integer
*/
public int firstMissingPositive(int[] A) {
// write your code here
if(A.length ==0)
return 1;
// if(A.length ==1){
// if(A[0]<= 0)
// return 1;
// return A[0]+1;
// }
// 1.找到正数的最小值
// 2.最小值>1 return 1
// 3.最小值<0 return 1
// 4.最小值是1,最小值向上加 1 不存在的话就是答案
int Min = Integer.MAX_VALUE;
int MinInt = Integer.MAX_VALUE;
ArrayList<Integer> list = new ArrayList<Integer>();
for(int i=0;i< A.length;i++){
if(A[i]>0){
list.add(A[i]);
if(A[i] < Min)
Min = A[i];
}
}
if(Min>1 )
return 1;
int i = 1;
// 最小值等于 1
while(i<= A.length){
if(!list.contains(Min + i)){
MinInt = Min + i;
break;
}
i = i + 1;
}
return MinInt;
}
}

Java Code

总耗时: 2210 ms

这个和寻找缺失的数好像很像,虽然改写后的程序,理解不透,但是LeetCode的测试样例,造成无法通过

样例:[1,1]这样会陷入死循环。

程序如下

public class Solution {
/**
* @param A: an array of integers
* @return: an integer
*/
public int firstMissingPositive(int[] A) {
// write your code here
if(A.length == 0)
return 1;
int n = A.length;
for(int i =0 ;i< n ;i++){
while(A[i] != i + 1){
if(A[i] <= 0 || A[i] > n)
break;
int tmp = A[i];
A[i] = A[tmp - 1];
A[tmp - 1] = tmp;
}
}
for(int i =0;i< n ;i++)
if( A[i] != i+ 1)
return i+1;
return n + 1;
}
}

Java Code

class Solution:
# @param A, a list of integers
# @return an integer
def firstMissingPositive(self, A):
# write your code here
if A == None:
return 1
n = len(A)
nums = A[:]
for i in range(n):
while A[i] != i+1:
nums = A[:]
if A[i]<=0 or A[i]>=n:
break
tmp = A[i]
A[i] = A[tmp-1]
A[tmp-1] = tmp
# print 'before:',nums
# print 'later:',A
for i in range(n):
if A[i]!= i +1:
return i+1
return n +1

Python Code

====更新

在LeetCode讨论中看到的程序

public class Solution {
/**
* @param A: an array of integers
* @return: an integer
*/
public int firstMissingPositive(int[] A) {
// write your code here
//int[] A={2,1};
if(A.length == 0)
return 1;
int n = A.length;
for(int i = 0; i < n; ++ i){
int digit = A[i];
while(digit > 0 && digit <= n && A[digit - 1] != digit){
int tmp = A[i];
A[i] = A[digit -1];
A[digit -1] =tmp;
digit = A[i];
} } for(int i = 0; i < n; ++ i)
if(A[i] != i + 1)
return i + 1; return n + 1;
}
}

这里的思想还是寻找缺失的数的思想

寻找缺失的数,原始序列是0 -N的,找出其中缺失的说,所以0是序列中的数,或者说0可能是答案

而这个题目是寻找第一个缺失的正整数,这里0不是答案,所以还要增加判断:A[A[i] - 1] !=A[i]

换个角度说就是让数组排序后是 1 2 3 4 的样式,第0个位置 应该是1 ,第1个位置应该是 2,第i个位置应该是i+1

对[3,4,-1,1]

第0个位置A[2]!=A[0] 交换后 :[-1,4,3,1]

第0个位置A[0]<0 跳出

第1个位置A[3]!=A[1]交换后:[-1,1,3,4]

第1个位置A[0]!=A[1]交换后:[1,-1,3,4]

第1个位置A[1]<0跳出

第2个位置A[2]==A[2]跳出

第3个位置A[3]==A[3]跳出

下面判断A[i] 是否等于i+1,第一个不满足条件的就是答案,i+1是答案,当都满足的时候就是n+1

Python

class Solution:
# @param A, a list of integers
# @return an integer
def firstMissingPositive(self, A):
# write your code here
if A == None:
return 1
n = len(A)
nums = A[:]
for i in range(n):
digit = A[i]
while digit>0 and digit<=n and A[digit -1] != digit:
A[i],A[digit -1] = A[digit-1],A[i]
digit = A[i]
for i in range(n):
if A[i]!= i +1:
return i+1
return n +1

说明:在交换的时候不用digit代替A[i] 交换无效。

lintcode:First Missing Positive 丢失的第一个正整数的更多相关文章

  1. [LeetCode] 41. First Missing Positive ☆☆☆☆☆(第一个丢失的正数)

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

  2. leetCode 41.First Missing Positive (第一个丢失的正数) 解题思路和方法

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

  3. LeetCode OJ:First Missing Positive (第一个丢失的正数)

    在leetCode上做的第一个难度是hard的题,题目如下: Given an unsorted integer array, find the first missing positive inte ...

  4. [leetcode]41. First Missing Positive第一个未出现的正数

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

  5. [Swift]LeetCode41. 缺失的第一个正数 | First Missing Positive

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

  6. LeetCode题解41.First Missing Positive

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

  7. LeetCode(41)First Missing Positive

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

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

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

  9. Leetcode First Missing Positive

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

随机推荐

  1. CSS各个浏览器Hack的写法

    Hack是针对不同的浏览器去写不同的CSS样式,从而让各浏览器能达到一致的渲染效果,那么针对不同的浏览器写不同的CSS CODE的过程,就叫CSS HACK,同时也叫写CSS Hack.然后将Hack ...

  2. Linq to Entities

    首先要添加一个ADO.NET实体数据模型 添加一个Entities 对象,其用法和linqtosql类似例如: StudentInfoEntities2 entity = new StudentInf ...

  3. openerp经典收藏 对象定义详解(转载)

    对象定义详解 原文地址:http://shine-it.net/index.php/topic,2159.0.htmlhttp://blog.sina.com.cn/s/blog_57ded94e01 ...

  4. 第一个leapmotion的小游戏

    自从看过leapmotion的宣传视频,就被吸引住了.觉得这东西迟早要替代鼠标,然后关注了一年多leapmotion的动态,终于在今年8月份入手了一只.//675大洋啊,心疼~ 一直想写份评测,一直想 ...

  5. linux 标准io笔记

    三种缓冲 1.全缓冲:在缓冲区写满时输出到指定的输出端. 比如对磁盘上的文件进行读写通常是全缓冲的. 2.行缓冲:在遇到'\n'时输出到指定的输出端. 比如标准输入和标准输出就是行缓冲, 回车后就会进 ...

  6. sql 对一张表进行按照不同条件进行多次统计

    最近一直在做数据统计,在此过程中,遇到过好多种情况都是对一张表按照不同的条件进行多次统计,以前的做法是统计几次按照不同的条件left join 几次,虽然也能得到想要的结果,但是效率太低,反映在页面就 ...

  7. Basic Operation about Linux

    1. 永久开启/关闭防火墙 在linux中防火墙是一个名叫iptables的工具 开启: chkconfig iptables on 关闭: chkconfig iptables off 即时生效,重 ...

  8. Python 学习教程

    <Core Python Programming>勘误参考表 http://starship.python.net/crew/wesc/cpp/errata2.htm 笨办法学 Pytho ...

  9. 【quartz】 数据库方式管理任务

    public static void Run(bool inClearJobs, bool inScheduleJobs) { var properties = new NameValueCollec ...

  10. Android存储机制之Preference

    Preference提供了一种轻量级的数据存取方法,主要是数据比较少的配置信息.它以键值对的方式将数据保存在一个XML配置文件中. 使用Preference方式来存取数据,用到了SharedPrefe ...