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 ...
随机推荐
- TensorFlow:tf.reduce_mean(input_tensor, axis=None, keep_dims=False, name=None, reduction_indices=None)
转载:https://www.cnblogs.com/yuzhuwei/p/6986171.html 1.概述 在深度学习里研究的物体的关系,都是比较复杂的.比如一个图片32X32大小的,它的像素信息 ...
- XML 中可嵌入 cmd命令脚本
原文要参照代码 1. XML解析 Task逻辑块可相互组合,形成复杂的树状结构,其结构用XML表示,即写成XML文件的形式. 样例如下: <!-- 顺序执行块 --> <seq> ...
- Window 10 :如何彻底关闭:Windows Defender Service(2015-12-20日更新)
Window 10 :如何彻底关闭:Windows Defender Service? 网上流传的什么组策略gpeidt.msc方法,什么安装其他的杀软之类的方法都很麻烦,且有弊病! 其实很简单: 利 ...
- 跨域、跨服务器调用时候session丢失的问题
最近新进一个公司,做的项目是手机支付系统.由于涉及到金钱相关的,所以安全性要求特别的高.项目分了很多个子系统,在部署(测试)的时候是每个Tomcat上面只放一个子系统.比如现在有5个子系统,那么就会对 ...
- python垃圾回收,判断内存占用,手动回收内存,二
以下为例子,判断计算机内存并释放程序内存. # coding=utf8 import time import psutil, gc, commands,os from logger_until imp ...
- [CNN] Face Detection
即将进入涉及大量数学知识的阶段,先读下“别人家”的博文放松一下. 读罢该文,基本能了解面部识别领域的整体状况. 后生可畏. 结尾的Google Facenet中的2亿数据集,仿佛隐约听到:“你们都玩儿 ...
- Linux命令缩写的全称
[目录|文件] ls : list(列出目录内容) pwd : print work directory(打印当前目录,现示当前工作目录的绝对路径) cd : change directory(改变目 ...
- nuget包循环引用问题
1.项目中有类库YesWay.Nlog.RabbitMQ,依赖项如下YesWay.Nlog.RabbitMQ=>YesWay.Service.Discovery=>YesWay.Log 2 ...
- Nexus5 破解电信关键步骤
5儿子终于摔坏了,送去保养之后,发现之前已破解的电信3G竟然无效了,心碎!!!!!!!!!!!!!!!!!! 尝试恢复efs --还好有备份,备份万岁!!! 不行!继续尝试恢复!还是不行!再试!... ...
- ckeditor4.4.6添加代码高亮
研究了很久才发现,在 ckeditor4.4.6中添加代码高亮超级简单啊,下面直接上过程 ckeditor4.4.6支持自定义代码高亮,利用Code Snippet插件并默认启用highlight.j ...