1 Star 0 Fork 0

nullptrh/Algorithm-Practice

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
_0041_FirstMissingPositive.cpp 1.01 KB
一键复制 编辑 原始数据 按行查看 历史
Restart20200301 提交于 2020-06-27 15:29 +08:00 . today practice
/*
给你一个未排序的整数数组,请你找出其中没有出现的最小的正整数。
 
示例 1:
输入: [1,2,0]
输出: 3
示例 2:
输入: [3,4,-1,1]
输出: 2
示例 3:
输入: [7,8,9,11,12]
输出: 1
 
提示:
你的算法的时间复杂度应为O(n),并且只能使用常数级别的额外空间。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/first-missing-positive
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
*/
// 算法思路:用负数来标记已存在的数字
class Solution {
public:
int firstMissingPositive(vector<int>& nums) {
int n = nums.size();
for (auto& i : nums)
if (i <= 0) i = n + 1;
for (auto& i : nums) {
if (abs(i) <= n) {
int num = abs(i) - 1;
nums[num] = -abs(nums[num]);
}
}
for (int i = 0; i < n; i++)
if (nums[i] > 0) return i + 1;
return n + 1;
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/nullptrh/Algorithm-Practice.git
git@gitee.com:nullptrh/Algorithm-Practice.git
nullptrh
Algorithm-Practice
Algorithm-Practice
master

搜索帮助