[leedcode 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.

public class Solution {
    //题目要求时间复杂度是O(n),解题思路:桶排序的变形,使得,每个数nums[i]等于i+1
    //swap的条件是要在数组范围内,且不为负数和0,且要交换的值没有在正确位置,否则就没必要交换了;而且交换值与被交换值不能相同,否则会死循环
    public int firstMissingPositive(int[] nums) {
        int i=0;
        while(i<nums.length){
            if(nums[i]>0&&nums[i]<nums.length&&nums[i]!=i+1&&nums[i]!=nums[nums[i]-1]){
                swap(nums,nums[i]-1,i);
            }else i++;
        }
        for(i=0;i<nums.length;i++){
            if(nums[i]!=i+1)  // 从头开始遍历,找到第一个不在对应位置上的元素
               return i+1;
        }
        return nums.length+1; // 说明所有元素都在正确的位置,那么只能返回数组长度后的第一个元素了
    }
    public void swap(int[] nums,int i,int j){
        int temp=nums[i];
        nums[i]=nums[j];
        nums[j]=temp;

    }

}
时间: 2025-01-12 07:30:29

[leedcode 41] First Missing Positive的相关文章

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,

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

[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

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题意:给定一个无序数组,保持数组不重复找出最小的正整数代码如

刷题41. First Missing Positive

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

[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

41. First Missing Positive(C++)

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. Solution: class Solution { public: int firstMissingP

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. 解题思路:本题的核心思想就是将x放置到nums[x-1]的位置上去,然后从头开始遍历数组即可.但是把数交

41. First Missing Positive (Map)

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. 思路:n个数,可能的最大正整数是n,所以可以将这n个数作为哈希值.但是这样要有O(n)的额外空间. 解决