mirror of
https://github.com/krahets/hello-algo.git
synced 2025-01-23 22:40:25 +08:00
Add a test and update ts code style
This commit is contained in:
parent
3265e3fde0
commit
4a31f909c6
@ -30,4 +30,13 @@ function twoSumHashTable(nums: number[], target: number): number[] {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return [];
|
return [];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/* Driver Code */
|
||||||
|
let nums = [2, 7, 11, 15]
|
||||||
|
twoSumBruteForce(nums, 9)
|
||||||
|
console.log("使用暴力枚举后得到结果:", nums)
|
||||||
|
|
||||||
|
let nums1 = [2, 7, 11, 15]
|
||||||
|
twoSumHashTable(nums1, 9)
|
||||||
|
console.log("使用辅助哈希表后得到结果", nums1)
|
@ -97,16 +97,16 @@ comments: true
|
|||||||
|
|
||||||
```typescript title="leetcode_two_sum.ts"
|
```typescript title="leetcode_two_sum.ts"
|
||||||
function twoSumBruteForce(nums: number[], target: number): number[] {
|
function twoSumBruteForce(nums: number[], target: number): number[] {
|
||||||
let n = nums.length;
|
let 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++) {
|
||||||
if (nums[i] + nums[j] === target) {
|
if (nums[i] + nums[j] === target) {
|
||||||
return [i, j]
|
return [i, j];
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
return [];
|
||||||
return [];
|
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user