leetcode-41-First Missing Positive

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zem_nezer/article/details/85086681

Base on the idea:
With the length of the array l, we can know that the result will be range from[1, l + 1]. E.g. Now we have array[1, 2, 3], the result will be 4, if array = [5, 4, 7], the result will be 1

So, we have use map to map our vector/array to the map, then you can imagine it is a reallocate process, which a weakly map a unsort struct to sotred struct.

Intuition:
At first I think we can sort the array then go through it, the time complexity is O(nlogn). So we need to use someway to decrease to O(n). Because we do not need to do anything with the array itself, we can use a map to store the array.

I think it is a very useful method when deal with other problem like this, which is not directly manipulate with array but need to use it.

Error:
N/A

猜你喜欢

转载自blog.csdn.net/zem_nezer/article/details/85086681