💡

100日アルゴリズム[7日目・hashTable]

2024/03/27に公開

解いた問題

https://leetcode.com/problems/two-sum/description/

回答

function twoSum(nums: number[], target: number): number[] {
    let hashmap = new Map()

    for (let i:number = 0; i < nums.length; i++) {
        let difference = target - nums[i];
        if(hashmap.has(difference)) return [hashmap.get(difference), i]
        hashmap.set(nums[i],i)
    }
};

Discussion