[LeetCode]First Missing Positive
来源:互联网 发布:人工智能的未来 豆瓣 编辑:程序博客网 时间:2024/05/05 01:18
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.
class Solution {public: int firstMissingPositive(vector<int>& nums) { //交换的方法 int len = nums.size(); for(int i=0; i<len; ++i){ if(nums[i]==i+1) continue; while(nums[i]!=i+1&&nums[i]>0&&nums[i]<nums.size()&&nums[i]!=nums[nums[i]-1]){ //交换直到直到出现无效的i位 //注意边界条件,和重复交换的情况 swap(nums[i],nums[nums[i]-1]); } } int i; for(i=0; i<len; ++i){ //找到第一个不匹配的数字 if(nums[i]!=i+1) break; } return i+1; } void swap(int &a,int &b){ int temp = a; a = b; b = temp; }};
0 0
- LeetCode: First Missing Positive
- LeetCode First Missing Positive
- LeetCode: First Missing Positive
- [Leetcode] First Missing Positive
- [LeetCode] First Missing Positive
- [Leetcode] First Missing Positive
- leetcode First Missing Positive
- LeetCode First Missing Positive
- [leetcode] first missing positive
- LeetCode: First Missing Positive
- [LeetCode]First Missing Positive
- LeetCode-First Missing Positive
- [leetcode] First Missing Positive
- [LeetCode] First Missing Positive
- LeetCode - First Missing Positive
- LeetCode | First Missing Positive
- Leetcode: First Missing Positive
- Leetcode:First Missing Positive
- 《代码大全2》读书笔记
- Linux延时函数
- a+b(高精度)
- 敏捷测试的方法和实践
- does not contain bitcode. You must rebuild it.....
- [LeetCode]First Missing Positive
- shader内置函数
- Kafka使用入门教程
- java精度问题
- ExtJs教程 3.0
- 项目中用到的正则表达式
- 计算机网络——数据链路层、网络层
- jqgrid 右键菜单实现
- VE接口-获取经纬度