给出一个未排序的数组,求出第一个丢失的正数。
给出组合为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: 
public class Solution41 {
public int firstMissingPositive(int[] A) {
// added line9 condition to avoid infinite loop
// added line13, the i--, to check the swapped item. Or we failed to check all the numbers.
// ref http://stackoverflow.com/questions/1586858/find-the-smallest-integer-not-in-a-list
if (A.length == 0) return 1;//长度等于0 直接返回1
for (int i = 0; i < A.length; i++) {//遍历
//是整数,小于数组长度,不等于当前下标
if (A[i] <= A.length && A[i] > 0 && A[i] != i+1) {
if (A[A[i]-1] != A[i]) { //line 9 进行交换操作
int tmp = A[A[i]-1];
A[A[i]-1] = A[i];
A[i] = tmp;
i--; //line 13
}
}
}
for (int i = 0; i < A.length; i++) {
if (A[i] != i+1) return i+1;
}
return A.length+1;
}
}

参考代码2:

package leetcode_50;

/***
*
* @author pengfei_zheng
* 找到第一个丢失的正数
*/
public class Solution41 {
public static int firstMissingPositive(int[] nums) {
int i = 0;
while(i < nums.length){
if(nums[i] == i+1 || nums[i] <= 0 || nums[i] > nums.length) i++;
else if(nums[nums[i]-1] != nums[i]) swap(nums, i, nums[i]-1);
else i++;
}
i = 0;
while(i < nums.length && nums[i] == i+1) i++;
return i+1;
} private static void swap(int[] A, int i, int j){
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
public static void main(String[]args){
int []nums = {1,1};
System.out.println(firstMissingPositive(nums));
}
}
 
 

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. spring mvc实现自定义注解

    实现方式:使用@Aspect实现: 1. 新建注解接口:CheckSign package com.soeasy.web.utils; import org.springframework.core. ...

  2. 最简单的视音频播放演示样例3:Direct3D播放YUV,RGB(通过Surface)

    ===================================================== 最简单的视音频播放演示样例系列文章列表: 最简单的视音频播放演示样例1:总述 最简单的视音频 ...

  3. [转]iOS证书(.p12)和描述文件(.mobileprovision)申请

    转载于:http://ask.dcloud.net.cn/article/152 iOS有两种证书和描述文件: 证书类型 使用场景 开发(Development)证书和描述文件 用于开发测试,在HBu ...

  4. 前端如何获取http状态码400的返回值

    axios.get("/check_mobile_and_sent_code",{withCredentials:true,params:{mobile:formInline.mo ...

  5. mysql中date_add()函数的使用?

    需求描述: 在使用mysql的过程中,需要对日期进行计算,比如对某个日期加上几天,几个小时等操作, 在此记录下,date_add()函数的使用. 操作过程: date_add()函数语法: DATE_ ...

  6. java 实现类似于python requests包的Session类,自动管理cookie。

    1.在py中requests.post()和get()函数都是在那个函数内部里面自动生成了一个Session类的实例,所以requests,post和get函数要想干登陆后才能干的事情,需要添加coo ...

  7. 06-Linux RPM 命令参数使用详解

    rpm 执行安装包二进制包(Binary)以及源代码包(Source)两种.二进制包可以直接安装在计算机中,而源代码包将会由 RPM自动编译.安装.源代码包经常以src.rpm作为后缀名. 常用命令组 ...

  8. 一个java程序员的年终总结

    年底了,该给自己写点总结了! 从毕业到现在已经快4年啦,一直在Java的WEB开发行业混迹.我不是牛人,但是自我感觉还算是个合格的程序员,有必要写下自己将近4年来的经历,给自我以提示,给刚入行的朋友提 ...

  9. Dubbo -- 系统学习 笔记 -- 成熟度

    Dubbo -- 系统学习 笔记 -- 目录 成熟度 功能成熟度 策略成熟度 啦啦啦

  10. 5 -- Hibernate的基本用法 --4 9 其他常用的配置属性

    Hibernate其他常用的配置属性: ⊙ hibernate.show_sql : 是否在控制台输出Hibernate持久化操作底层所使用的SQL语句.只能为true和false两个值. ⊙ hib ...