First Missing Positive Given an unsorted integer array, find the first missing positive integer. Fo
来源:互联网 发布:北电网络交换 编辑:程序博客网 时间:2024/05/16 16:59
First Missing Positive
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.
Java代码:
public class Solution { public int firstMissingPositive(int[] A) { if (A == null || A.length == 0) { return 1; } // Put the corresponding positive value in the index that equals to its value int len = A.length; for (int i = 0; i < len;) { int num = A[i]; if (num > 0 && num < len && num != i && num != A[num]) { A[i] = A[num]; A[num] = num; } else { ++i; } } // Scan the array to find the first value that is not equal to its index, then it is the missing value // Test small examples first int missingValue = A[0] == len ? len+1 : len; for (int i = 1; i < len; ++i) { if (A[i] != i) { missingValue = i; break; } } return missingValue;}}
0 0
- First Missing Positive Given an unsorted integer array, find the first missing positive integer. Fo
- Given an unsorted integer array, find the first missing positive integer.
- Find the smallest positive number missing from an unsorted array
- [LeetCode]041-First Missing Positive Integer
- 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
- nutch1.6与mongoDB的轻度结合
- xlslib中使用中文字体
- linux文件同步工具使用
- Undefined symbols for architecture i386:
- [css]CSS如何让DIV的宽度随内容的改变而改变
- First Missing Positive Given an unsorted integer array, find the first missing positive integer. Fo
- Android开发中的drawable-(hdpi,mdpi,ldpi)和WVGA,HVGA,QVGA的区别以及联系
- linux 脚本的开发步骤
- ThreadLocal深入理解 修订版
- 1602液晶显示屏
- 面向站长和网站管理员的Web缓存加速指南
- 通俗讲解:缓存、缓存算法和缓存框架
- 百炼OJ 2972:确定进制
- 浏览器部分补充