Hard-题目29:41. First Missing Positive

原创 2016年05月31日 23:40:19

题目原文:
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.
题目大意:
给出一个未排序的整形数组,寻找第一个缺失的整数。
题目分析:
(这真是原创的)用hashset就好了,判断集合中是否有n却没有n+1.
源码:(language:java)

public class Solution {
    public int firstMissingPositive(int[] nums) {
        HashSet<Integer> set = new HashSet<Integer>();
        int max=0;
        for(int num:nums) {
            set.add(num);
            if(num>max)
                max=num;
        }
        if(!set.contains(1))
            return 1;
        for(int i=1;i<max;i++) {
            if(set.contains(i)&&!set.contains(i+1))
                return i+1;
        }
        return max+1;
    }

}
成绩:
3ms,3.77%,1ms,82.72%
cmershen的碎碎念:
题目要求O(n)复杂度和O(1)空间复杂度,但这个算法用到了hashset,看网上的题解似乎是一种基于桶排序的算法,我也不理解。

版权声明:完整版Leetcode题解请出门左转https://github.com/cmershen1/leetcode/tree/master/docs

[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

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

41. First Missing Positive

Task: Given an unsorted integer array, find the first missing positive integer. For example, Giv...

41. First Missing Positive\48. Rotate Image

First Missing Positive 题目描述 代码实现 Rotate Image 题目描述 代码实现41. First Missing Positive题目描述Given an unsort...

[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(java)

问题描述: Given an unsorted integer array, find the first missing positive integer.For example, Given ...
  • zdavb
  • zdavb
  • 2015年07月10日 22:18
  • 471

LeetCode41/19 First Missing Positive/Remove Nth Node From End of List ****

leetcode41 First Missing Positive 题目: Given an unsorted integer array, find the first missing posi...

41. First Missing Positive 找到第一个缺失的正整数

给一个未排序的整数序列:找出第一个缺失的正整数。Given [1,2,0] return 3, and [3,4,-1,1] return 2. 分析:我们可以忽略到那些 这道题如果是不重复的序列的话...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Hard-题目29:41. First Missing Positive
举报原因:
原因补充:

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