We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 8a0c699 commit 1793dd6Copy full SHA for 1793dd6
missing-number/taewanseoul.ts
@@ -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