mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 04:31:55 +08:00
Add JS for chapter of computational complexity
This commit is contained in:
@ -90,7 +90,17 @@ comments: true
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="leetcode_two_sum.js"
|
||||
|
||||
function twoSumBruteForce(nums, target) {
|
||||
let n = nums.length;
|
||||
// 两层循环,时间复杂度 O(n^2)
|
||||
for (let i = 0; i < n; i++) {
|
||||
for (let j = i + 1; j < n; j++) {
|
||||
if (nums[i] + nums[j] === target) {
|
||||
return [i, j]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "TypeScript"
|
||||
@ -193,7 +203,18 @@ comments: true
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="leetcode_two_sum.js"
|
||||
|
||||
function twoSumHashTable(nums, target) {
|
||||
// 辅助哈希表,空间复杂度 O(n)
|
||||
let m = {}
|
||||
// 单层循环,时间复杂度 O(n)
|
||||
for (let i = 0; i < nums.length; i++) {
|
||||
if (m[nums[i]] !== undefined) {
|
||||
return [m[nums[i]], i]
|
||||
} else {
|
||||
m[target - nums[i]] = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "TypeScript"
|
||||
|
||||
Reference in New Issue
Block a user