First Missing Positive
来源:互联网 发布:windows.old能不能删除 编辑:程序博客网 时间:2024/06/06 13:02
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.
Solution:
class Solution {public: int firstMissingPositive(vector<int>& nums) { int len = nums.size(); for(int i = 0; i < len; ++i) { while(nums[i] != i + 1 && nums[i] > 0 && nums[i] <= len && nums[i] != nums[nums[i]-1]) { swap(nums[nums[i]-1], nums[i]); } } int i = -1; while(++i < len) { if(nums[i] != i + 1) break; } return i + 1; }};
0 0
- LeetCode: First Missing Positive
- LeetCode First Missing Positive
- LeetCode: First Missing Positive
- [Leetcode] First Missing Positive
- leetcode12: First Missing Positive
- First Missing Positive
- [LeetCode] First Missing Positive
- [Leetcode] First Missing Positive
- leetcode First Missing Positive
- LeetCode First Missing Positive
- [leetcode] first missing positive
- First Missing Positive
- LeetCode: First Missing Positive
- First Missing Positive
- First Missing Positive
- [LeetCode]First Missing Positive
- First Missing Positive
- First Missing Positive
- Catch That Cow
- 暑假集训第四周周三赛F - Knight Moves 骑士的移动 STL
- Android 4.4 Graphic Architecture
- LeetCode(12)Integer to Roman
- 后缀表达式计算
- First Missing Positive
- R 学习资源汇总
- nutch 2.2.1 InjectorJob 类
- Android Studio系列教程一--下载与安装
- javascript高级编程技术 第三章
- mybatis 懒加载
- CodeForces 462C Appleman and Toastman 贪心
- jvm扫盲
- Android Bitmap大量使用不产生OOM之“加载大图片资源优化”