mirror of
https://github.com/krahets/hello-algo.git
synced 2025-02-03 07:01:55 +08:00
Update ts code and docs
This commit is contained in:
parent
45aca03b92
commit
84caa60cd4
@ -5,7 +5,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
function twoSumBruteForce(nums: number[], target: number): number[] {
|
function twoSumBruteForce(nums: number[], target: number): number[] {
|
||||||
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++) {
|
||||||
@ -33,10 +33,12 @@ function twoSumHashTable(nums: number[], target: number): number[] {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/* 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 nums1 = [2, 7, 11, 15], target1 = 9;
|
let res = twoSumBruteForce(nums, target);
|
||||||
twoSumHashTable(nums1, target1)
|
console.log("方法一 res = ", res);
|
||||||
console.log("使用辅助哈希表后得到结果", nums1)
|
|
||||||
|
// 方法二
|
||||||
|
res = twoSumHashTable(nums, target);
|
||||||
|
console.log("方法二 res = ", res);
|
@ -97,7 +97,7 @@ 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;
|
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++) {
|
||||||
|
Loading…
Reference in New Issue
Block a user