LeetCode 41. First Missing Positive
来源:互联网 发布:团圆之后知乎 编辑:程序博客网 时间:2024/06/05 17:53
描述
给出一个未排序的数组,找出丢失的数,从1开始的
解决
遍历数组,交换数,使得每个数对应索引号。最后再遍历看哪一个数不对,返回即可。
class Solution {public: int firstMissingPositive(vector<int>& nums) { int length = nums.size(); for (int i = 0; i < length; ++i) { while (nums[i] > 0 && nums[i] < length && nums[i] != i + 1 && nums[i] != nums[nums[i] - 1]) swap(nums[i], nums[nums[i] - 1]); } for (int i = 0; i < length; ++i) { if (i + 1 != nums[i]) return i + 1; } return length + 1; }};
0 0
- [LeetCode]41.First Missing Positive
- LeetCode --- 41. First Missing Positive
- LeetCode 41.First Missing Positive
- [Leetcode] 41. First Missing Positive
- [leetcode] 41.First Missing Positive
- leetCode 41. First Missing Positive
- LeetCode 41. First Missing Positive
- leetcode 41. First Missing Positive
- Leetcode 41. First Missing Positive
- LeetCode 41. First Missing Positive
- [leetcode]41. First Missing Positive
- LeetCode-41.First Missing Positive
- leetcode 41. First Missing Positive
- [LeetCode] 41. First Missing Positive
- [LeetCode]--41. First Missing Positive
- leetcode 41. First Missing Positive
- leetcode 41.First Missing Positive
- leetcode 41. First Missing Positive
- 利用pyqt4编写cs计算工具
- git Please tell me who you are解决方法
- 深度学习-----思想篇(一)
- IDEA入门级
- Linux USB子系统(2) 设备、配置、接口、端点
- LeetCode 41. First Missing Positive
- jQuery ajax jsonp实现跨域请求
- (第三季)605-unity聊天室-客户端发送消息到服务器端
- 利用Python Pandas进行数据预处理-数据清洗
- Sublime Text 3 LESS、SCSS高亮插件
- 第三十五讲项目3——Bessel函数
- JavaScript高级程序设计笔记-面向对象的编程
- SQL语句笔记
- JAVA枚举的作用与好处