2014.2.8 23:43
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.
Solution1:
The first solution I thought of was hashing, using unordered_map. This promises O(n) time, but not constant space.
Solution is simple and runs in one pass. I guess the code can explain itself.
Time and space complexities are both O(n).
Accepted code:
1 // 1AC, unordered_map is a good substitute for map 2 #include <unordered_map> 3 using namespace std; 4 5 class Solution { 6 public: 7 int firstMissingPositive(int A[], int n) { 8 if (A == nullptr || n <= 0) { 9 return 1; 10 } 11 12 unordered_map<int, int> um; 13 int result; 14 int i; 15 16 result = 1; 17 for (i = 0; i < n; ++i) { 18 if (A[i] > result) { 19 um[A[i]] = 1; 20 } else if (A[i] < result) { 21 continue; 22 } else { 23 um[result] = 1; 24 ++result; 25 while (um.find(result) != um.end()) { 26 ++result; 27 } 28 } 29 } 30 31 um.clear(); 32 return result; 33 } 34 };
Solution2:
The last solution used hashing, thus cannot satisfy the constraint on space usage.
Note that there're n integers in [1, n], if there exists some A[i] out of this range, one of the positive integers from [1, n] must be missing, which is the answer we're looking for. If no such A[i] exists, (n + 1) will be the first missing positive integer.
Since we care only about positive integers, we can use "-" sign to mark a position, as a way of hashing. If we mark A[i] as minus, it means the positive number i has already appeared. You don't have to worry about i going out of range, because the size of the hash table is exactly n. The array itself serves as the hash table, while the sign of each element is used for hashing, whereas their absolute values are preserved.
Time complexity is O(n). Space complexity is O(1).
Accepted code:
1 // 1CE, 1AC, this solution is very tricky, hope it would inspire you. 2 class Solution { 3 public: 4 int firstMissingPositive(int A[], int n) { 5 if (A == nullptr || n <= 0) { 6 return 1; 7 } 8 9 int i; 10 for (i = 0; i < n; ++i) { 11 if (A[i] <= 0) { 12 A[i] = n + 1; 13 } 14 } 15 16 int tmp; 17 for (i = 0; i < n; ++i) { 18 if (myabs(A[i]) <= n) { 19 // think about why we care about values between [1, n] 20 tmp = myabs(A[i]); 21 // the array itself serves as a hash table of size n. 22 // setting it to minus means occupying this position. 23 A[tmp - 1] = -myabs(A[tmp - 1]); 24 } 25 } 26 27 for (i = 0; i < n; ++i) { 28 if (A[i] > 0) { 29 return (i + 1); 30 } 31 } 32 // this array consists of [1, n]. 33 return (n + 1); 34 } 35 private: 36 int myabs(const int x) { 37 return (x >= 0 ? x : -x); 38 } 39 };
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)