LeetCode: First Missing Positive
来源:互联网 发布:淘宝网怎么找货源 编辑:程序博客网 时间:2024/06/05 08:58
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(int A[], int n) { for(int i = 0; i < n; i++) { if(A[i] == i+1 || A[i] > n || A[i] <= 0 || A[A[i]-1] == A[i]) continue; int temp = A[A[i]-1]; A[A[i]-1] = A[i]; A[i] = temp; i--; } for(int i = 0; i < n; i++) { if(A[i] != i+1) return i+1; } return n+1; }};
Round 2;
class Solution {public: int firstMissingPositive(int A[], int n) { if(n == 0) return 1; for(int i = 0; i < n; i++) { if(A[i] <= 0 || A[i] > n || A[i] == i+1 || A[A[i]-1] == A[i]) continue; else { int temp = A[A[i]-1]; A[A[i]-1] = A[i]; A[i] = temp; i--; } } for(int i = 0; i < n; i++) { if(A[i] != i+1) return i+1; } return n+1; }};
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
- php 获取ip
- VC程序修改程序图标随记
- GTK入门学习:布局容器之水平布局
- VS2012 MFC + OpenCV
- Android应用开发相关下载资源(2014/12/14更新) 【转】
- LeetCode: First Missing Positive
- Cassandra因为OOM(Out Of Memeory)被kernel直接kill掉的问题
- Largest Number
- 数据在Access与Office组件之间如何自由流动
- iOS基础面试题(一)
- sizeof 实现
- Android客户端和服务器端数据交互的第四种方法
- 数据备份中容易出错的情况及解决办法
- 深入理解SetUID