LeetCode OJ:First Missing Positive (第一个丢失的正数)

在leetCode上做的第一个难度是hard的题,题目如下:

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(N)的时间复杂度以及常数级别的空间复杂度,先贴上我写的函数,完全不能达到上面的要求,智能实现NlgN的时间复杂度:

 1 class Solution {
 2 public:
 3     int firstMissingPositive(vector<int>& nums) {
 4         sort(nums.begin(), nums.end());
 5         int sz = nums.size();
 6         if(sz == 0) return 1;
 7         int index;
 8         for (index = 0; index < sz; index++){
 9             if (nums[index] <= 0)
10                 continue;
11             else
12                 break;
13         }
14         if (nums[index] != 1 || index == sz) return 1;//当没有正数的情况或正数的第一个数不是1的情况
15         while (index < sz){
16             if (nums[index + 1] != nums[index] && nums[index + 1] != nums[index] + 1) //两个判断主要是为了防止vector中重复的数字出现。
17                 return nums[index] + 1;
18             index++;
19         }
20         return nums[index] + 1;
21     }
22 };

由于达不到时间以及空间复杂度的要求,实在想不出来,我去看了下别人写的,现在由于vector可能会出现重复的数,我暂时不知带怎样去解决,只有先这样,回头有时间再回来填坑。

时间: 2024-12-29 16:11:39

LeetCode OJ:First Missing Positive (第一个丢失的正数)的相关文章

leetCode 41.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. 思路:这个题刚開始是没有思路的,难就难在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. 题解分析: 如果是T(n)的时间 T(n)的空间,则

【leetcode】 First Missing Positive

[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. 找到第一个没有出现的正整数 思路:

[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

【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][JavaScript]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. https://leetcode.com/problems

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. 分析: 因为数组的大小为n,因此那个缺失的整数只可能的范围[1,n+1] 方法一:需要O(n)的空间,设

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

Java for LeetCode 041 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. 解题思路一: 刚看到题目的时候感觉无从下手,后来仔细理解题意,需要找到first missing p