LeetCode 41 First Missing Positive(找到数组中第一个丢失的正数)
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
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(找到数组中第一个丢失的正数)的更多相关文章
- [array] leetcode - 41. First Missing Positive - Hard
leetcode - 41. First Missing Positive - Hard descrition Given an unsorted integer array, find the fi ...
- LeetCode - 41. First Missing Positive
41. First Missing Positive Problem's Link ---------------------------------------------------------- ...
- [LeetCode] 41. First Missing Positive 首个缺失的正数
Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...
- leetcode 41 First Missing Positive ---java
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- Java [Leetcode 41]First Missing Positive
题目描述: Given an unsorted integer array, find the first missing positive integer. For example,Given [1 ...
- [LeetCode] 41. First Missing Positive ☆☆☆☆☆(第一个丢失的正数)
Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...
- leetCode 41.First Missing Positive (第一个丢失的正数) 解题思路和方法
First Missing Positive Given an unsorted integer array, find the first missing positive integer. Fo ...
- [leetcode]41. First Missing Positive第一个未出现的正数
Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...
- [Leetcode][Python]41: First Missing Positive
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 41: First Missing Positivehttps://oj.le ...
随机推荐
- C# Http访问帮助类,支持get post请求文件下载 [
using System; using System.Collections.Generic; using System.Collections.Specialized; using System.I ...
- c fopen fread 错误
真的被,读取一个txt文本,结果一个早上都没搞好 程序如下: 能看出哪里有问题么,输出字符串,得到的结果后面有“屯”或则 “烫”,单个字符输出来也有,为何,搜啊搜,改txt的内容,依旧不行 最后 改f ...
- Memcached系列之一
安装.运行 memcached -h 启动选项: -d 作为后台程序 -m -u -l -p -c -P (1)作为前台程序运行 memcached -vv // 显示调试信息 official do ...
- MySql数据库恢复(*frm)文件
mysql数据库恢复(*frm)文件 WorkBench 在使用虚拟服务器时,服务器提供商一般不会像我们使用本地数据库一样:使用导入导出(这样的文件后缀是*.sql).大部分时候提供的是一个文件夹,里 ...
- linux cfs调度器_模型实现
调度器真实模型的主要成员变量及与抽象模型的对应关系 I.cfs_rq结构体 a) struct sched_entity *curr 指向当前正在执行的可调度实体.调度器的调度单位 ...
- 【Deep Learning】RNN LSTM 推导
http://blog.csdn.net/Dark_Scope/article/details/47056361 http://blog.csdn.net/hongmaodaxia/article/d ...
- jquery绑定事件的坑,重复绑定问题
我实现点击table表格中的删除按钮,找到当前按钮的祖先元素tr 然后删除该行,但是我首先点击删除的时候要先弹出提示框,是否要下载,这时在点击删除按钮删除,之前没有考虑到事件重复绑定问题,所以每次点击 ...
- mysql查询两个日期之间相差多少天?
需求描述: 在mysql中,查看两个日期之间相差多少天 操作过程: 1.通过datediff函数,查看两个日期之间相差多少天 mysql> select datediff('2018-06-26 ...
- MongoDB中常用的find
接着前一篇文章,下面主要介绍一下MongoDB中常用的find操作. 先打开MongoDB shell,通过下面一组命令插入一些数据. post1 = {} post2 = {} post3 = {} ...
- Netty权威指南之Netty入门程序
package com.hjp.netty.netty; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.Chan ...