Update js code and docs
This commit is contained in:
parent
c8da48c0d6
commit
dc985cb962
@ -5,7 +5,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
function twoSumBruteForce(nums, target) {
|
function twoSumBruteForce(nums, target) {
|
||||||
let n = nums.length;
|
const n = nums.length;
|
||||||
// 两层循环,时间复杂度 O(n^2)
|
// 两层循环,时间复杂度 O(n^2)
|
||||||
for (let i = 0; i < n; i++) {
|
for (let i = 0; i < n; i++) {
|
||||||
for (let j = i + 1; j < n; j++) {
|
for (let j = i + 1; j < n; j++) {
|
||||||
@ -14,6 +14,7 @@ function twoSumBruteForce(nums, target) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
function twoSumHashTable(nums, target) {
|
function twoSumHashTable(nums, target) {
|
||||||
@ -27,13 +28,15 @@ function twoSumHashTable(nums, target) {
|
|||||||
m[target - nums[i]] = i;
|
m[target - nums[i]] = i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Driver Code */
|
/* Driver Code */
|
||||||
let nums = [2, 7, 11, 15], target = 9;
|
// 方法一
|
||||||
twoSumBruteForce(nums, target)
|
const nums = [2, 7, 11, 15], target = 9;
|
||||||
console.log("使用暴力枚举后得到结果:", nums)
|
let res = twoSumBruteForce(nums, target)
|
||||||
|
console.log("方法一 res = ", res)
|
||||||
|
|
||||||
let nums1 = [2, 7, 11, 15], target1 = 9;
|
// 方法二
|
||||||
twoSumHashTable(nums1, target1)
|
res = twoSumHashTable(nums, target)
|
||||||
console.log("使用辅助哈希表后得到结果", nums1)
|
console.log("方法二 res = ", res)
|
||||||
|
@ -91,7 +91,7 @@ comments: true
|
|||||||
|
|
||||||
```js title="leetcode_two_sum.js"
|
```js title="leetcode_two_sum.js"
|
||||||
function twoSumBruteForce(nums, target) {
|
function twoSumBruteForce(nums, target) {
|
||||||
let n = nums.length;
|
const n = nums.length;
|
||||||
// 两层循环,时间复杂度 O(n^2)
|
// 两层循环,时间复杂度 O(n^2)
|
||||||
for (let i = 0; i < n; i++) {
|
for (let i = 0; i < n; i++) {
|
||||||
for (let j = i + 1; j < n; j++) {
|
for (let j = i + 1; j < n; j++) {
|
||||||
@ -100,6 +100,7 @@ comments: true
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return [];
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -214,6 +215,7 @@ comments: true
|
|||||||
m[target - nums[i]] = i;
|
m[target - nums[i]] = i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return [];
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user