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 59e8a48 commit be71d9fCopy full SHA for be71d9f
missing-number/mike2ox.ts
@@ -0,0 +1,15 @@
1
+/**
2
+ * source: https://leetcode.com/problems/missing-number/
3
+ * 풀이방법: 0부터 n까지의 합에서 주어진 배열의 합을 빼면 빠진 숫자를 구할 수 있음
4
+ *
5
+ * 시간복잡도: O(n) (n: nums의 길이)
6
+ * 공간복잡도: O(1) (상수 공간만 사용)
7
+ */
8
+
9
+function missingNumber(nums: number[]): number {
10
+ const n = nums.length;
11
+ let expectedSum = (n * (n + 1)) / 2; // 0부터 n까지의 합 공식
12
+ let realSum = nums.reduce((sum, cur) => sum + cur, 0);
13
14
+ return expectedSum - realSum;
15
+}
0 commit comments