Skip to content

Commit ae4c7d6

Browse files
committed
[Array] Add a solution to First Missing Positive
1 parent 1980d89 commit ae4c7d6

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

Array/FirstMissingPositive.swift

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/first-missing-positive/
3+
* Primary idea: Use a set to hold number in the array and iterate through 1...nums.count to find the missing one
4+
* Time Complexity: O(n), Space Complexity: O(n)
5+
*
6+
*/
7+
8+
class FirstMissingPositive {
9+
func firstMissingPositive(_ nums: [Int]) -> Int {
10+
var set = Set<Int>()
11+
12+
nums.forEach { set.insert($0) }
13+
14+
for i in 0..<nums.count {
15+
if !set.contains(i + 1) {
16+
return i + 1
17+
}
18+
}
19+
20+
return nums.count + 1
21+
}
22+
}

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* [Microsoft](#microsoft)
2929

3030
## Progress
31-
[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 269 completed solutions. Note: questions with &hearts; mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).
31+
[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 270 completed solutions. Note: questions with &hearts; mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).
3232

3333

3434
## Array
@@ -40,6 +40,7 @@
4040
[Island Perimeter](https://leetcode.com/problems/island-perimeter/)| [Swift](./Array/IslandPerimeter.swift)| Easy| O(nm)| O(1)|
4141
[Majority Element](https://leetcode.com/problems/majority-element/)| [Swift](./Array/MajorityElement.swift)| Easy| O(n)| O(1)|
4242
[Majority Element II](https://leetcode.com/problems/majority-element-ii/)| [Swift](./Array/MajorityElementII.swift)| Medium| O(n)| O(1)|
43+
[First Missing Positive](https://leetcode.com/problems/first-missing-positive/)| [Swift](./Array/FirstMissingPositive.swift)| Hard| O(n)| O(n)|
4344
[Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/)| [Swift](./Array/IntersectionTwoArrays.swift)| Easy| O(n)| O(n)|
4445
[Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/)| [Swift](./Array/IntersectionTwoArraysII.swift)| Easy| O(n)| O(n)|
4546
[Contains Duplicate](https://leetcode.com/problems/contains-duplicate/)| [Swift](./Array/ContainsDuplicate.swift)| Easy| O(n)| O(n)|
@@ -787,7 +788,7 @@
787788
| [Swift](./DP/WildcardMatching.swift) | 44 | [Wildcard Matching](https://oj.leetcode.com/problems/wildcard-matching/) | Hard |
788789
| [Swift](./String/MultiplyStrings.swift) | 43 | [Multiply Strings](https://oj.leetcode.com/problems/multiply-strings/) | Medium |
789790
| [Swift](./Math/TrappingRainWater.swift) | 42 | [Trapping Rain Water](https://oj.leetcode.com/problems/trapping-rain-water/) | Hard |
790-
| | 41 | [First Missing Positive](https://oj.leetcode.com/problems/first-missing-positive/) | Hard |
791+
| [Swift](./Array/FirstMissingPositive.swift) | 41 | [First Missing Positive](https://oj.leetcode.com/problems/first-missing-positive/) | Hard |
791792
| [Swift](./DFS/combinationSumII.swiftc) | 40 | [Combination Sum II](https://oj.leetcode.com/problems/combination-sum-ii/) | Medium |
792793
| [Swift](./DFS/CombinationSum.swift) | 39 | [Combination Sum](https://oj.leetcode.com/problems/combination-sum/) | Medium |
793794
| [Swift](./String/CountAndSay.swift) | 38 | [Count and Say](https://oj.leetcode.com/problems/count-and-say/) | Easy |

0 commit comments

Comments
 (0)