[LeetCode] [First Missing Positive 2012-03-08]

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

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

class
Solution {

public:

    

    void
LinkMove(int
A[], int
i, int
n)

    {

        if(A[i] > n || A[i] <= 0)   

        {

            A[i] = -1;

            return;

        }

        if(A[i] == i+1)   return;

        

        int
s = A[i]-1;

        

        int
tmp = A[s];

        A[s] = A[i];

        A[i] = tmp;

        if(A[s] != A[i] ) //should be check, or there will be a circle call

        {

            LinkMove(A, i, n);

        }

    }

    int
firstMissingPositive(int
A[], int
n) {

        for(int
i = 0;i<n;i++)

        {

            LinkMove(A, i, n);

        }

        for(int
i = 0; i< n; i++)

        {

            if(A[i] <= 0 || A[i] != i+1) return
i+1; 

        }

        return
n+1;

    }

};

[LeetCode] [First Missing Positive 2012-03-08]

时间: 2024-08-05 10:01:55

[LeetCode] [First Missing Positive 2012-03-08]的相关文章

[leetcode]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. 算法分析: 将1 ~ a.length的数字放到与下标相应

leetcode First Missing Positive hashset简单应用

1 public class Solution { 2 public int firstMissingPositive(int[] A) { 3 HashSet<Integer> hash=new HashSet<Integer>(); 4 int count=0; 5 int sum=0; 6 7 for(int i:A) 8 { 9 if(i>0) 10 { 11 hash.add(i); 12 } 13 } 14 15 int beg=1; 16 while(hash.

Leetcode: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. 题解分析: 如果是T(n)的时间 T(n)的空间,则

LeetCode: First Missing Positive [040]

[题目] 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: 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. SOLUTION 1: 使用类似桶排序的方法: 将值放在它

[LeetCode] 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——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. 方法: 既然是正整数,那么可以考虑将正整数跟下标对应起来存放.正整数不包括0,则下标0存放1

leetcode First Missing Positive python

class Solution(object): def firstMissingPositive(self, nums): """ :type nums: List[int] :rtype: int """ intlen=len(nums) i=0 while i < intlen: if nums[i] > 0 and nums[i] < intlen and nums[i] != nums[nums[i]-1]: #get

leetcode:First Missing Positive分析和实现

题目大意: 传入整数数组nums,求nums中未出现的正整数中的最小值.要求算法在O(n)时间复杂度内实现,并且只能分配常量空间. 分析: 一般碰到这种问题,都先对数组进行排序,再遍历数组就可以找到最小的在nums中没有出现过的正整数.但是由于排序的时间复杂度一般为O(nlog2(n)),因此时间复杂度没有达到要求. 之后再转回排序的方式,有一种排序的方式称为桶排序,只要有足够的空间,就可以在O(n)的时间复杂度内完成排序过程.但事实是只能分配常量空间. 只能分配常量空间还要求时间复杂度为O(n