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.contains(beg))
17          {
18              beg++;
19
20
21          }
22
23          return beg;
24
25
26
27     }
28 }

V

 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.contains(beg))
17          {
18              beg++;
19
20
21          }
22
23          return beg;
24
25
26
27     }
28 }

w Code

leetcode First Missing Positive hashset简单应用,布布扣,bubuko.com

时间: 2024-10-23 04:30:18

leetcode First Missing Positive hashset简单应用的相关文章

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

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 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

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 首个缺失的正数

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