41. First Missing Positive【leetcode】寻找第一个丢失的整数,java,算法

41. First Missing Positive

Given an unsorted integer array, find the first missing positive integer.

For example,
Given [1,2,0] return 3,
and [3,4,-1,1] return 2.

Your algorithm should run in O(n) time and uses constant space.

题目:寻找第一个丢失的整数意思为【1,2,4,5,6,7】那么丢失的是3,如果【1,1000】,这时丢失的是2

注意需要在O(n)时间内而且不允许使用额外的空间

所以题目是需要你在从1 ~ +∞的范围中,找到没有在输入数组出现过的最小的正public class Solution {    public int firstMissingPositive(int[] nums) {        int len=nums.length;if(nums==null||len==0)

return 1;
        //首先判断当前元素是否为整数,否则直接过,并且num[j]<len不然造成数组越界,最后判断如果nums[j]-1!=[j]防止死循环
        for(int j=0;j<len;j++){
            while(nums[j]>0&&nums[j]<=len&&(nums[j]-1)!=j){
                int temp=nums[nums[j]-1];
                if(temp==nums[j]){
                    break;
                }
                nums[nums[j]-1]=nums[j];
                nums[j]=temp;
            }
        }
        for(int j=0;j<len;j++){
            if(nums[j]!=j+1){
               return j+1;
            }
        }             

  return len+1;
    }
}
时间: 2024-12-25 16:08:35

41. First Missing Positive【leetcode】寻找第一个丢失的整数,java,算法的相关文章

41. First Missing Positive Leetcode Python

Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] return 3, and [3,4,-1,1] return 2. Your algorithm should run in O(n) time and uses constant space. 这题用的是count sort 的方法. 首先我们来复习一下count sort 已知一个正整数还未

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

leetcode - 41. First Missing Positive - Hard descrition Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] return 3, and [3,4,-1,1] return 2. Your algorithm should run in O(n) time and uses constant s

LeetCode - 41. First Missing Positive

41. First Missing Positive Problem's Link ---------------------------------------------------------------------------- Mean: 给你一组整数,找出第一个空缺的正整数. 要求:时间O(n),空间O(n). analyse: 这题时间O(n)想了半天没想到,用O(n*logn)过的. 然后看了discuss,想法非常巧妙,自愧不如. Time complexity: O(N) v

刷题41. First Missing Positive

一.题目说明 题目是41. First Missing Positive,求一个未排序队列中缺失的最小正整数.时间复杂度要求是O(n).难度是Hard,确实难. 二.我的解答 不考虑时间复杂度,首先对队列进行排序,然后从第一个正数开始,如果不是1就返回1,否则继续查找2....找不到就返回,找到就继续. 代码如下: #include<iostream> #include<vector> #include<algorithm> #include<unordered_

First Missing Positive leetcode java

题目: Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] return 3, and [3,4,-1,1] return 2. Your algorithm should run in O(n) time and uses constant space. 题解: 题目给了一个unsorted integer array.当然,可以用先排好序的方法

41. First Missing Positive(js)

41. First Missing Positive Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2,0] Output: 3 Example 2: Input: [3,4,-1,1] Output: 2 Example 3: Input: [7,8,9,11,12] Output: 1题意:给定一个无序数组,保持数组不重复找出最小的正整数代码如

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

First Missing Positive Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] return 3, and [3,4,-1,1] return 2. Your algorithm should run in O(n) time and uses constant space. 思路:这个题刚開始是没有思路的,难就难在O(n)时间内

[Leetcode][Python]41: First Missing Positive

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 41: First Missing Positivehttps://oj.leetcode.com/problems/first-missing-positive/ Given an unsorted integer array, find the first missing positive integer.For example,Given [1,2,0] return 3,a

【一天一道LeetCode】#41. First Missing Positive

一天一道LeetCode系列 (一)题目 Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] return 3, and [3,4,-1,1] return 2. Your algorithm should run in O(n) time and uses constant space. (二)解题 /* 首先对初始vector进行排序,然后设定