Skip to main content

1. Two Sum

分析

用 memo 来记录

题解

function twoSum(nums: number[], target: number): number[] {
const memo: Record<number, number> = {}
for (let i = 0; i < nums.length; i++) {
const need = target - nums[i]
if (memo[need] !== undefined) {
return [i, memo[need]]
}
memo[nums[i]] = i
}
};