-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1.两数之和.js
54 lines (50 loc) · 1.07 KB
/
1.两数之和.js
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
* @lc app=leetcode.cn id=1 lang=javascript
*
* [1] 两数之和
*/
// @lc code=start
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function(nums, target) {
let temp = []
for (let index = 0; index < nums.length; index++) {
let diff = target - nums[index]
if (temp[diff] !== undefined) {
return [temp[diff], index]
}
temp[nums[index]] = index
}
};
// @lc code=en
// 解法一: hash表, 时间复杂度On, 空间复杂度On
var twoSum = function (nums, target) {
let hash = {}
for (let i = 0; i < nums.length; i++) {
let dif = target - nums[i]
if (hash[nums[i]] !== undefined) {
return [hash[nums[i]], i]
}
hash[dif] = i
}
}
var nums = [2, 7, 11, 15]
console.log(twoSum(nums, 9))
/*
{
7: 0
}
*/
// // 解法二: 暴力法, 时间复杂度On2, 空间复杂度O1
var twoSum = function (nums, target) {
for (let i = 0; i < nums.length; i++) {
for (let j = i + 1; j < nums.length; j++ ) {
if (nums[i] + nums[j] === target) {
return [i, j]
}
}
}
}