diff --git a/LeetcodeProblems/Algorithms/2Sum.js b/LeetcodeProblems/Algorithms/2Sum.js new file mode 100644 index 0000000..44df5cd --- /dev/null +++ b/LeetcodeProblems/Algorithms/2Sum.js @@ -0,0 +1,43 @@ +/** +2 Sum +https://leetcode.com/problems/two-sum/ + +Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. + +You may assume that each input would have exactly one solution, and you may not use the same element twice. + +You can return the answer in any order. + +Example 1: +Input: nums = [2,7,11,15], target = 9 +Output: [0,1] +Explanation: Because nums[0] + nums[1] == 9, we return [0, 1]. + +Example 2: +Input: nums = [3,2,4], target = 6 +Output: [1,2] + +Example 3: +Input: nums = [3,3], target = 6 +Output: [0,1] + +*/ + +/** + * @param {number[]} nums + * @param {number} target + * @return {number[]} + */ + var twoSum = function(nums, target) { + let map ={}; + for(let i=0;i` (e.g. | [Binary Gap ](/LeetcodeProblems/Algorithms/Binary_Gap.js) | Easy | https://leetcode.com/problems/binary-gap/ | | [Binary Gap ](/LeetcodeProblems/Algorithms/Binary_Gap.js) | Easy | https://leetcode.com/problems/binary-gap/ | | [Majority Element](/LeetcodeProblems/Algorithms/Majority_Element.js) | Easy | https://leetcode.com/problems/majority-element/ | +| [Two Sum](/LeetcodeProblems/Algorithms/2Sum.js) | Easy | https://leetcode.com/problems/two-sum/ | | [Tic Tac Toe ](/LeetcodeProblems/Algorithms/Tic_Tac_Toe.js) | | | | [Permutations With Duplicates ](/LeetcodeProblems/Algorithms/Permutations_With_Duplicates.js) | | | | [Deletion Distance](/LeetcodeProblems/Algorithms/Deletion_Distance.js) | | |