[leetcode-41]First Missing Positive(java)

原创 2015年07月10日 22:18:31

问题描述:
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开始,看看缺少了哪个整数,缺少的第一个就是要返回的值。难点在于要求常数的空间使用,下面给出两种方法,第一种严格意义上讲是错误的,因为它的空间复杂度为O(n)但是很简洁。使用的是hashset。

第二种做法是常数时间复杂度。想想,O(n)的时间复杂度,也即扫描一遍就应该能找到缺少的值,很自然的想到hash的思想。这里面的大多数字都应该是连续的。否则意义不大,所以将每个元素都做一个hash,然后将其存放在对应的位置上。这样就能初步排成一个序列,而且时间复杂度为O(n),当然做hash就肯定会出现冲突,不过对于此问题而言,冲突是没有关系的,想想看,如果有n个元素,hash到n个筐子里面,如果存在冲突,那么肯定会有某个筐子是空的,那就好了,我们就找到了这个元素。

代码1:

    public int firstMissingPositive(int[] nums) {
        HashSet<Integer> set = new HashSet<Integer>();
        int length = nums.length;
        if(length<=0)
            return 1;

        for(int i = 0;i<length;i++){
            set.add(nums[i]);
        }

        int index = 1;
        while(set.contains(index++));
        return index-1;
     }

代码2:

public int firstMissingPositive(int[] nums){
        int length = nums.length;
        if(length<=0)
            return 1;
        int i;
        int index,nextIndex;
        int current,next;
        for(i = 0;i<length;i++)
            if(nums[i]<0)
                nums[i] = 0;
        for(i = 0;i<length;i++){
            current = nums[i];
            index = current%length;
            if(i == index)
                continue;
            next = nums[index];
            nextIndex = next%length;

            while(nextIndex!=index){
                nums[index] = current;
                current = next;
                index = nextIndex;
                next = nums[index];
                nextIndex = next%length;
            }
            if(current<nums[index]&&current>0)
                nums[index] = current;
        }
        i = 1;
        while(nums[i%length]==i)i++;
        return i;
    }

方案2(C版本)

int firstMissingPositive(int* nums, int numsSize) {
        int length = numsSize;
        if(length<=0)
            return 1;
        int i;
        int index,nextIndex;
        int current,next;
        for(i = 0;i<length;i++)
            if(nums[i]<0)
                nums[i] = 0;
        for(i = 0;i<length;i++){
            current = nums[i];
            index = current%length;
            if(i == index)
                continue;
            next = nums[index];
            nextIndex = next%length;

            while(nextIndex!=index){
                nums[index] = current;
                current = next;
                index = nextIndex;
                next = nums[index];
                nextIndex = next%length;
            }
            if(current<nums[index]&&current>0)
                nums[index] = current;
        }
        i = 1;
        while(nums[i%length]==i)i++;
        return i;
}

代码1的耗时:348 ms,代码2的耗时:280 ms,然后我把代码2用C语言重写了一遍,耗时0ms,可见,java比C的效率要低太多。

版权声明:

相关文章推荐

DAY28:leetcode #41 First Missing Positive

Given an unsorted integer array, find the first missing positive integer. For example, Given [1,...

Leetcode NO.41 First Missing Positive

本题题目要求如下: Given an unsorted integer array, find the first missing positive integer. For exam...

【LeetCode】41. First Missing Positive

问题描述https://leetcode.com/problems/first-missing-positive/#/descriptionGiven an unsorted integer arra...

leetcode 41. First Missing Positive

1.题目 Given a collection of candidat...

Leetcode41. First Missing Positive

Leetcode First Missing Positive

LeetCode OJ-41-First Missing Positive

题目:Given an unsorted integer array, find the first missing positive integer. For example, Given [1...

[leetcode]41. First Missing Positive,C++/python实现,hard难度

[leetcode]41. First Missing Positive,C++/python实现,hard难度

[LeetCode - 桶排序] 41. First Missing Positive

1 问题 Given an unsorted integer array, find the first missing positive integer. For example, ...

leetcode题解-41. First Missing Positive

题目:Given an unsorted integer array, find the first missing positive integer.For example, Given [1,2,...

leetcode 41. First Missing Positive

import java.util.Arrays; //Given an unsorted integer array, find the first missing positive integer...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)