Skip to content

Commit be71d9f

Browse files
committed
feat: Upload missing-number(typescript)
1 parent 59e8a48 commit be71d9f

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

missing-number/mike2ox.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)