First Missing Positive
来源:互联网 发布:京东云 阿里云 编辑:程序博客网 时间:2024/05/02 04:37
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.
每次交换使得一个数据num回到num-1的位置
class Solution {public: int firstMissingPositive(int A[], int n) { for(int i=0;i<n;){ if(A[i]!=i+1&&A[i]<=n&&A[i]>0&&A[A[i]-1]!=A[i]) { int temp=A[A[i]-1]; A[A[i]-1]=A[i]; A[i]=temp; } else i++; } int ans; for(ans=0;ans<n;ans++) if(A[ans]!=ans+1) break; return ans+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
- unity 高仿汽车灯光的实现
- 根据传入的size创建数组
- 批处理、脚本
- 数据库权限设计
- web.xml 配置404和500错误的自定义页面
- First Missing Positive
- android addRule() 的作用!
- SSH之Spring——IOC如何实现依赖注入
- jsp中 中文处理
- SQL中CONVERT转化函数的用法
- 码农提高工作效率
- JavaSe基础XX18——IO流_4
- 利用博客提交作业的程序设计课程实践模式
- UVALive - 3027Corporative Network(带权并查集)