xinyu04

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

[Oracle] LeetCode 41 First Missing Positive 思维

Given an unsorted integer array nums, return the smallest missing positive integer.

You must implement an algorithm that runs in O(n) time and uses constant extra space.

Solution

对于 nums[i] 它对应的位置应该是 nums[nums[i]1]

点击查看代码
class Solution {
public:
int firstMissingPositive(vector<int>& nums) {
int n = nums.size();
for(int i=0;i<n;i++){
while(nums[i]>0&& nums[i]<=n && nums[i]!=nums[nums[i]-1])swap(nums[i], nums[nums[i]-1]);
}
for(int i=0;i<n;i++)
if(nums[i]!=i+1)return i+1;
return n+1;
}
};

posted on   Blackzxy  阅读(19)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
点击右上角即可分享
微信分享提示