题目描述
给定一个无重复元素的整数数组nums,请你找出其中没有出现的最小的正整数 要求: 空间复杂度 O(1),时间复杂度 O(n)
核心代码
int minNumberDisappeared(vector<int>& nums) {
// write code here
int n=nums.size();
for(auto & x:nums)
{
if(x<=0) x=n+1;
//cout<<x<<endl;
}
for(auto x:nums)
{
if(abs(x)<=n) nums[abs(x)-1]*=-1;
}
for(int i=0;i<n;++i)
{
if(nums[i]>0) return i+1;
}
return n+1;
}