Skip to content

Commit d21f643

Browse files
committed
[Math] Add a solution to Missing Number
1 parent dc1036f commit d21f643

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

Math/MissingNumber.swift

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/missing-number/
3+
* Primary idea: The distinct number is the sum from 0 to expected end number, which is
4+
* exactly the array length, minus the sum of all the values in array
5+
* Time Complexity: O(n), Space Complexity: O(1)
6+
*
7+
*/
8+
9+
class MissingNumber {
10+
func missingNumber(_ nums: [Int]) -> Int {
11+
return nums.count * (nums.count + 1) / 2 - nums.reduce(0, +)
12+
}
13+
}

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
![Leetcode](./logo.png?style=centerme)
55

66
## Progress
7-
[Problem Status](#problem-status) shows the latest progress to all 1000+ questions. Currently we have 277 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them.
7+
[Problem Status](#problem-status) shows the latest progress to all 1000+ questions. Currently we have 278 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them.
88

99
## Contributors
1010

@@ -281,6 +281,7 @@
281281
[Add Two Numbers](https://leetcode.com/problems/add-two-numbers/)| [Swift](./Math/AddTwoNumbers.swift)| Medium| O(n)| O(1)|
282282
[Add Digits](https://leetcode.com/problems/add-digits/)| [Swift](./Math/AddDigits.swift)| Easy| O(1)| O(1)|
283283
[Plus One](https://leetcode.com/problems/plus-one/)| [Swift](./Math/PlusOne.swift)| Easy| O(n)| O(1)|
284+
[Missing Number](https://leetcode.com/problems/missing-number/)| [Swift](./Math/MissingNumber.swift)| Easy| O(n)| O(1)|
284285
[Divide Two Integers](https://leetcode.com/problems/divide-two-integers/)| [Swift](./Math/DivideTwoIntegers.swift)| Medium| O(logn)| O(1)|
285286
[Number Complement](https://leetcode.com/problems/number-complement/)| [Swift](./Math/NumberComplement.swift)| Easy| O(n)| O(1)|
286287
[Hamming Distance](https://leetcode.com/problems/hamming-distance/)| [Swift](./Math/HammingDistance.swift)| Easy| O(n)| O(1)|
@@ -597,7 +598,7 @@
597598
| | 271 | [Encode and Decode Strings](https://leetcode.com/problems/encode-and-decode-strings/) ♥ | Medium |
598599
| [Swift](./Search/ClosestBinarySearchTreeValue.swift) | 270 | [Closest Binary Search Tree Value](https://leetcode.com/problems/closest-binary-search-tree-value/) ♥ | Easy |
599600
| [Swift](./Sort/AlienDictionary.swift) | 269 | [Alien Dictionary](https://leetcode.com/problems/alien-dictionary/) ♥ | Hard |
600-
| | 268 | [Missing Number](https://leetcode.com/problems/missing-number/) | Medium |
601+
| [Swift](./Math/MissingNumber.swift) | 268 | [Missing Number](https://leetcode.com/problems/missing-number/) | Easy |
601602
| | 267 | [Palindrome Permutation II](https://leetcode.com/problems/palindrome-permutation-ii/) ♥ | Medium |
602603
| [Swift](./String/PalindromePermutation.swift) | 266 | [Palindrome Permutation](https://leetcode.com/problems/palindrome-permutation/) ♥ | Easy |
603604
| | 265 | [Paint House II](https://leetcode.com/problems/paint-house-ii/) ♥ | Hard |

0 commit comments

Comments
 (0)