给出一个未排序的数组,求出第一个丢失的正数。
给出组合为int []nums = {0,1,3,-1,2,5};
下面为按照参考代码1执行交换运算的输出结果
 
0 1 3 -1 2 5
1 0 3 -1 2 5
1 0 3 -1 2 5
1 0 3 -1 2 5
1 0 3 -1 2 5
1 2 3 -1 0 5
1 2 3 -1 0 5
1 2 3 -1 5 0
1 2 3 -1 5 0
4
 
参考代码1: 
  1. public class Solution41 {
  2. public int firstMissingPositive(int[] A) {
  3. // added line9 condition to avoid infinite loop
  4. // added line13, the i--, to check the swapped item. Or we failed to check all the numbers.
  5. // ref http://stackoverflow.com/questions/1586858/find-the-smallest-integer-not-in-a-list
  6. if (A.length == 0) return 1;//长度等于0 直接返回1
  7. for (int i = 0; i < A.length; i++) {//遍历
  8. //是整数,小于数组长度,不等于当前下标
  9. if (A[i] <= A.length && A[i] > 0 && A[i] != i+1) {
  10. if (A[A[i]-1] != A[i]) { //line 9 进行交换操作
  11. int tmp = A[A[i]-1];
  12. A[A[i]-1] = A[i];
  13. A[i] = tmp;
  14. i--; //line 13
  15. }
  16. }
  17. }
  18. for (int i = 0; i < A.length; i++) {
  19. if (A[i] != i+1) return i+1;
  20. }
  21. return A.length+1;
  22. }
  23. }

参考代码2:

  1. package leetcode_50;
  2.  
  3. /***
  4. *
  5. * @author pengfei_zheng
  6. * 找到第一个丢失的正数
  7. */
  8. public class Solution41 {
  9. public static int firstMissingPositive(int[] nums) {
  10. int i = 0;
  11. while(i < nums.length){
  12. if(nums[i] == i+1 || nums[i] <= 0 || nums[i] > nums.length) i++;
  13. else if(nums[nums[i]-1] != nums[i]) swap(nums, i, nums[i]-1);
  14. else i++;
  15. }
  16. i = 0;
  17. while(i < nums.length && nums[i] == i+1) i++;
  18. return i+1;
  19. }
  20.  
  21. private static void swap(int[] A, int i, int j){
  22. int temp = A[i];
  23. A[i] = A[j];
  24. A[j] = temp;
  25. }
  26. public static void main(String[]args){
  27. int []nums = {1,1};
  28. System.out.println(firstMissingPositive(nums));
  29. }
  30. }
 
 

LeetCode 41 First Missing Positive(找到数组中第一个丢失的正数)的更多相关文章

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

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

  2. LeetCode - 41. First Missing Positive

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

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

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

  4. leetcode 41 First Missing Positive ---java

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

  5. Java [Leetcode 41]First Missing Positive

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

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

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

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

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

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

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

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

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

随机推荐

  1. Windows下安装Oracle12C(一)

    1,在官网上下载oracle的压缩文件,两个都要下载. 并两个同时选中解压在一个文件夹里面. 2,解压之后,如下图,点击setup.exe稍等一会儿 ,3,开始安装: 不选点击下一步,或者直接点击下一 ...

  2. lkl风控.随机森林模型测试代码spark1.6

    /** * Created by lkl on 2017/10/9. */ import org.apache.spark.sql.hive.HiveContext import org.apache ...

  3. mocha框架下,异步测试代码错误造成的问题----用例超时错误

    今天用抹茶(mocha)做个测试,发现有一个测试项目总是超时: describe("DbFactory functions",function(){ it("query ...

  4. ARC介绍

    从Ray Wenderlich的教程中截取了一小段作为对objective c中ARC的介绍,讲得比较清晰,原文有丰富的例子,见此 它是怎么工作的 你大概已经熟悉如何手工管理内存了, 就像这样:如果你 ...

  5. redis 的hash数据类型

    hash的常用命令 1.hset hset key field value 将哈希表key中的域field的值设为value 如果key不存在,一个新的哈希表被创建并进行HSET操作 如果field是 ...

  6. CCProxy序列号及注册码

    CCProxy无限用户版序列号:JHEHIHCDDAHC 注册码:15f7f78febfaee55afeafefff7cb7fdfb3

  7. Synycovery 7.18f 一个优秀的同步软件

    Serial Key Name: Vdown RG Code: MCKOFA7MNGUQY7954

  8. git 使gitnore立即生效

    由于之前有些需要过滤的文件已经提交到版本库了,之后再想起来添加时候已经晚了,使用如下方法 Git忽略规则和.gitignore规则不生效的解决办法   Git忽略规则: 在git中如果想忽略掉某个文件 ...

  9. 详解js中的apply与call的用法

    前言 call 和 apply 都是为了改变某个函数运行时的 context 即上下文而存在的,换句话说,就是为了改变函数体内部 this 的指向.call 和 apply二者的作用完全一样,只是接受 ...

  10. Java实现窗体动态加载磁盘文件

    在使用图形界面操作系统时,当打开一个文件夹系统会自动列出该文件夹下的所有文件及子文件夹.本实例实现了类似的功能:首先让用户选择一个文件夹,程序会动态列出该文件夹下的所有文件:如果该文件是隐藏文件,就在 ...