fueanta

LeetCode 01: Two Sum

Jun 30th, 2021
690
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function twoSum(nums: number[], target: number): number[] {
  2.     const seenEntries = new Map<number, number>();
  3.    
  4.     for (let i = 0; i < nums.length; i++) {
  5.         const complement = target - nums[i];
  6.        
  7.         if (seenEntries.has(complement))
  8.             return [seenEntries.get(complement)!, i];
  9.        
  10.         seenEntries.set(nums[i], i);
  11.     }
  12.    
  13.     return [];
  14. };
RAW Paste Data