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 ...
随机推荐
- Windows下安装Oracle12C(一)
1,在官网上下载oracle的压缩文件,两个都要下载. 并两个同时选中解压在一个文件夹里面. 2,解压之后,如下图,点击setup.exe稍等一会儿 ,3,开始安装: 不选点击下一步,或者直接点击下一 ...
- lkl风控.随机森林模型测试代码spark1.6
/** * Created by lkl on 2017/10/9. */ import org.apache.spark.sql.hive.HiveContext import org.apache ...
- mocha框架下,异步测试代码错误造成的问题----用例超时错误
今天用抹茶(mocha)做个测试,发现有一个测试项目总是超时: describe("DbFactory functions",function(){ it("query ...
- ARC介绍
从Ray Wenderlich的教程中截取了一小段作为对objective c中ARC的介绍,讲得比较清晰,原文有丰富的例子,见此 它是怎么工作的 你大概已经熟悉如何手工管理内存了, 就像这样:如果你 ...
- redis 的hash数据类型
hash的常用命令 1.hset hset key field value 将哈希表key中的域field的值设为value 如果key不存在,一个新的哈希表被创建并进行HSET操作 如果field是 ...
- CCProxy序列号及注册码
CCProxy无限用户版序列号:JHEHIHCDDAHC 注册码:15f7f78febfaee55afeafefff7cb7fdfb3
- Synycovery 7.18f 一个优秀的同步软件
Serial Key Name: Vdown RG Code: MCKOFA7MNGUQY7954
- git 使gitnore立即生效
由于之前有些需要过滤的文件已经提交到版本库了,之后再想起来添加时候已经晚了,使用如下方法 Git忽略规则和.gitignore规则不生效的解决办法 Git忽略规则: 在git中如果想忽略掉某个文件 ...
- 详解js中的apply与call的用法
前言 call 和 apply 都是为了改变某个函数运行时的 context 即上下文而存在的,换句话说,就是为了改变函数体内部 this 的指向.call 和 apply二者的作用完全一样,只是接受 ...
- Java实现窗体动态加载磁盘文件
在使用图形界面操作系统时,当打开一个文件夹系统会自动列出该文件夹下的所有文件及子文件夹.本实例实现了类似的功能:首先让用户选择一个文件夹,程序会动态列出该文件夹下的所有文件:如果该文件是隐藏文件,就在 ...