Skip to content

Commit dc985cb

Browse files
committed
Update js code and docs
1 parent c8da48c commit dc985cb

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

codes/javascript/chapter_computational_complexity/leetcode_two_sum.js

+10-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
function twoSumBruteForce(nums, target) {
8-
let n = nums.length;
8+
const n = nums.length;
99
// 两层循环,时间复杂度 O(n^2)
1010
for (let i = 0; i < n; i++) {
1111
for (let j = i + 1; j < n; j++) {
@@ -14,6 +14,7 @@ function twoSumBruteForce(nums, target) {
1414
}
1515
}
1616
}
17+
return [];
1718
}
1819

1920
function twoSumHashTable(nums, target) {
@@ -27,13 +28,15 @@ function twoSumHashTable(nums, target) {
2728
m[target - nums[i]] = i;
2829
}
2930
}
31+
return [];
3032
}
3133

3234
/* Driver Code */
33-
let nums = [2, 7, 11, 15], target = 9;
34-
twoSumBruteForce(nums, target)
35-
console.log("使用暴力枚举后得到结果:", nums)
35+
// 方法一
36+
const nums = [2, 7, 11, 15], target = 9;
37+
let res = twoSumBruteForce(nums, target)
38+
console.log("方法一 res = ", res)
3639

37-
let nums1 = [2, 7, 11, 15], target1 = 9;
38-
twoSumHashTable(nums1, target1)
39-
console.log("使用辅助哈希表后得到结果", nums1)
40+
// 方法二
41+
res = twoSumHashTable(nums, target)
42+
console.log("方法二 res = ", res)

docs/chapter_computational_complexity/space_time_tradeoff.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ comments: true
9191

9292
```js title="leetcode_two_sum.js"
9393
function twoSumBruteForce(nums, target) {
94-
let n = nums.length;
94+
const n = nums.length;
9595
// 两层循环,时间复杂度 O(n^2)
9696
for (let i = 0; i < n; i++) {
9797
for (let j = i + 1; j < n; j++) {
@@ -100,6 +100,7 @@ comments: true
100100
}
101101
}
102102
}
103+
return [];
103104
}
104105
```
105106

@@ -214,6 +215,7 @@ comments: true
214215
m[target - nums[i]] = i;
215216
}
216217
}
218+
return [];
217219
}
218220
```
219221

0 commit comments

Comments
 (0)