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.
题目内容: 给定一个无序的整形数组nums,找出数组中第一个缺失的正整数。 例如给出[1, 2, 0]这个数组,数组中存在连续的正整数1,2所以缺失的第一个正整数是3。 对于数组[3, 4, -1, 1],包含正整数1,3,4,缺少了1后面的2。
解题思路: 因为题目要求是找出第一个不连续的正整数,所以很容易想到,对于非正整数我们可以直接忽略掉。整个解题步骤可以分为3个步骤:
(1) 找出数组中非正整数的数量
找出数组中的非正整数可以简单地通过一个循环来实现,非正整数的数量作为startIndex
(2) 通过元素之间的换位将数组分成两大部分,非正整数和整数,如下图:
这个数组之间的换位是为了使得当num[i]为正整数的时候,nums[i] == i - startIndex + 1,实际上就是例如 当1存在数组里,nums[startIndex + 0] == 1; 当2存在数组里,nums[startIndex + 1] == 2; …… 当正整数n存在数组里,nums[startIndex + n - 1] == n; 也就是说,我们要从头开始遍历数组,遍历到一个正整数的时候,就要将这个正整数换位到相应的位置,但是也不是全部正整数都需要换位置,总的来说,对于数组nums的第i元素,同时满足以下4个条件的时候才需要换位置:
(3) 从startIndex开始遍历数组,找到第一个数组下标与该下标的值不匹配的位置,既是第一个不连续的正整数。 经过上一步的处理后,就可以再通过一次遍历来查找第一个缺失的正整数了。因为可能的第一个正整数是1,经过上述的处理后,如果1存在数组里,1应该会放在startIndex的位置,所以只需要从startIndex开始遍历数组。当找到第一个下标与该下标的元素值不对应的位置或者已经遍历到数组的结尾了,则可以认为(上一个位置的值+1)就是第一个缺失的正整数。但是当第一个正整数1就不存在的话,如果我们把(上一个位置的值+1)作为第一个缺失的正整数是不合理的,因为startIndex前面位置的元素是没有意义的。所以当startIndex位置的元素已经不匹配,那么直接返回1就可以了。
代码:
#include <vector>#include <iostream>using namespace std;class Solution {public: int firstMissingPositive(vector<int>& nums) { if (nums.size() == 0) { return 1; } int startIndex = 0; int nums_index = 0; vector<int>::iterator it = nums.begin(); while (it != nums.end()) { if (*it <= 0) { startIndex++; } it++; } while (nums_index < nums.size()) { if (nums[nums_index] > 0 && nums_index != startIndex + nums[nums_index] - 1 && startIndex + nums[nums_index] - 1 < nums.size() && nums[startIndex + nums[nums_index] - 1] != nums[nums_index]) { swap(nums, nums_index, startIndex + nums[nums_index] - 1); } else { nums_index++; } } it = nums.begin() + startIndex; while (it != nums.end()) { if (*it != (it - nums.begin()) - startIndex + 1) { if (it == nums.begin() + startIndex) { return 1; } return (*(it - 1)) + 1; } it++; } return (*(it - 1)) + 1; } void swap(vector<int>& nums, int x, int y) { int temp = nums[x]; nums[x] = nums[y]; nums[y] = temp; }};int main(int argc, char** argv) { int iarray[] = {3, 4, -1, 1}; int count = sizeof(iarray) / sizeof(int); vector<int> v(iarray, iarray+count); Solution sln; cout << sln.firstMissingPositive(v) << endl; return 0;}新闻热点
疑难解答