mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-25 15:06:30 +08:00
Update ts code style
This commit is contained in:
parent
dbb663703c
commit
671e8f56bf
2 changed files with 32 additions and 32 deletions
|
@ -2,33 +2,33 @@
|
||||||
* @Author: gyt95 (gytkwan@gmail.com)
|
* @Author: gyt95 (gytkwan@gmail.com)
|
||||||
* @Date: 2022-12-15 11:26:38
|
* @Date: 2022-12-15 11:26:38
|
||||||
* @Last Modified by: gyt95 (gytkwan@gmail.com)
|
* @Last Modified by: gyt95 (gytkwan@gmail.com)
|
||||||
* @Last Modified time: 2022-12-15 11:38:22
|
* @Last Modified time: 2022-12-15 23:46:25
|
||||||
*/
|
*/
|
||||||
|
|
||||||
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 [];
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function twoSumHashTable(nums: number[], target: number): number[] {
|
function twoSumHashTable(nums: number[], target: number): number[] {
|
||||||
// 辅助哈希表,空间复杂度 O(n)
|
// 辅助哈希表,空间复杂度 O(n)
|
||||||
let m: Map<number, number> = new Map()
|
let m: Map<number, number> = new Map();
|
||||||
// 单层循环,时间复杂度 O(n)
|
// 单层循环,时间复杂度 O(n)
|
||||||
for (let i = 0; i < nums.length; i++) {
|
for (let i = 0; i < nums.length; i++) {
|
||||||
let index = m.get(nums[i])
|
let index = m.get(nums[i]);
|
||||||
if (index !== undefined) {
|
if (index !== undefined) {
|
||||||
return [index, i]
|
return [index, i];
|
||||||
} else {
|
} else {
|
||||||
m.set(target - nums[i], i)
|
m.set(target - nums[i], i);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
return [];
|
||||||
return [];
|
|
||||||
};
|
};
|
|
@ -211,18 +211,18 @@ comments: true
|
||||||
|
|
||||||
```typescript title="leetcode_two_sum.ts"
|
```typescript title="leetcode_two_sum.ts"
|
||||||
function twoSumHashTable(nums: number[], target: number): number[] {
|
function twoSumHashTable(nums: number[], target: number): number[] {
|
||||||
// 辅助哈希表,空间复杂度 O(n)
|
// 辅助哈希表,空间复杂度 O(n)
|
||||||
let m: Map<number, number> = new Map()
|
let m: Map<number, number> = new Map();
|
||||||
// 单层循环,时间复杂度 O(n)
|
// 单层循环,时间复杂度 O(n)
|
||||||
for (let i = 0; i < nums.length; i++) {
|
for (let i = 0; i < nums.length; i++) {
|
||||||
let index = m.get(nums[i])
|
let index = m.get(nums[i]);
|
||||||
if (index !== undefined) {
|
if (index !== undefined) {
|
||||||
return [index, i]
|
return [index, i];
|
||||||
} else {
|
} else {
|
||||||
m.set(target - nums[i], i)
|
m.set(target - nums[i], i);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
return [];
|
||||||
return [];
|
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue