Skip to content

Commit 1793dd6

Browse files
committed
missing-number
1 parent 8a0c699 commit 1793dd6

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

missing-number/taewanseoul.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* 268. Missing Number
3+
* Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array.
4+
*
5+
* https://leetcode.com/problems/missing-number/description/
6+
*/
7+
8+
// O(n) time
9+
// O(1) space
10+
function missingNumber(nums: number[]): number {
11+
const n = nums.length;
12+
let total = (n * (n + 1)) / 2;
13+
14+
let sum = 0;
15+
for (let i = 0; i < n; i++) {
16+
sum += nums[i];
17+
}
18+
19+
return total - sum;
20+
}

0 commit comments

Comments
 (0)