Update ts code style

This commit is contained in:
gyt95 2022-12-15 23:48:03 +08:00
parent dbb663703c
commit 671e8f56bf
2 changed files with 32 additions and 32 deletions

View file

@ -2,7 +2,7 @@
* @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[] {
@ -11,7 +11,7 @@ function twoSumBruteForce(nums: number[], target: number): number[] {
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];
} }
} }
} }
@ -20,14 +20,14 @@ function twoSumBruteForce(nums: number[], target: number): number[] {
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 [];

View file

@ -212,14 +212,14 @@ 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 [];