forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblossssom.ts
More file actions
39 lines (35 loc) Β· 869 Bytes
/
blossssom.ts
File metadata and controls
39 lines (35 loc) Β· 869 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* @param nums - μ μ λ°°μ΄
* @param target - nums κ°μ λν΄ λμ¬ κ°
* @returns - targetμ λ§λλ index κ°
*
* @description
* 1. λμΌν μμ λλ² μ¬μ© κΈμ§
* 2. κ° μ
λ ₯μ λν΄ λͺ
νν νλμ μ루μ
μ΄ μλ€κ³ κ°μ
*
* @answer1
* - O(n^2)
*
* @answer2
* - O(n)
*/
// function twoSum(nums: number[], target: number): number[] {
// for (let i = 0; i < nums.length - 1; i++) {
// for (let j = i + 1; j < nums.length; j++) {
// if (nums[i] + nums[j] === target) {
// return [i, j];
// }
// }
// }
// return [];
// }
function twoSum(nums: number[], target: number): number[] {
const map = new Map();
for (let i = 0; i < nums.length; i++) {
if (map.has(target - nums[i])) {
return [map.get(target - nums[i]), i];
}
map.set(nums[i], i);
}
return [];
}