From 83bae76d35db1b308cf2ed10bf8e89d807d6f620 Mon Sep 17 00:00:00 2001 From: ist187644 Date: Sat, 23 Oct 2021 10:36:25 +0100 Subject: [PATCH 1/3] Added two sum solution with the problem description --- LeetCode/Problems/Python/2_Two_Sum.py | 47 +++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 LeetCode/Problems/Python/2_Two_Sum.py diff --git a/LeetCode/Problems/Python/2_Two_Sum.py b/LeetCode/Problems/Python/2_Two_Sum.py new file mode 100644 index 00000000..a0896509 --- /dev/null +++ b/LeetCode/Problems/Python/2_Two_Sum.py @@ -0,0 +1,47 @@ +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + dict = {} + + for i, num in enumerate(nums): + if(num in dict): + return [dict[num], i] + dict[target-num] = i + + # we do not need to return because we assume that we have + # have exactly one solution + # return -1 + +''' +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] +Output: 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] + + + +Constraints: + + 2 <= nums.length <= 104 + -109 <= nums[i] <= 109 + -109 <= target <= 109 + Only one valid answer exists. +''' \ No newline at end of file From df0959481186d7987f5b469717ca043b5c1ebc81 Mon Sep 17 00:00:00 2001 From: ist187644 Date: Sat, 23 Oct 2021 10:43:33 +0100 Subject: [PATCH 2/3] Corrected CodeFactor issues --- LeetCode/Problems/Python/2_Two_Sum.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/LeetCode/Problems/Python/2_Two_Sum.py b/LeetCode/Problems/Python/2_Two_Sum.py index a0896509..22be149f 100644 --- a/LeetCode/Problems/Python/2_Two_Sum.py +++ b/LeetCode/Problems/Python/2_Two_Sum.py @@ -1,17 +1,17 @@ class Solution: - def twoSum(self, nums: List[int], target: int) -> List[int]: - dict = {} + def twoSum(self, nums, target): + targets_dict = {} for i, num in enumerate(nums): - if(num in dict): - return [dict[num], i] - dict[target-num] = i + if(num in targets_dict): + return [targets_dict[num], i] + targets_dict[target-num] = i - # we do not need to return because we assume that we have - # have exactly one solution + # we do not need to return because we assume that + # we have exactly one solution # return -1 -''' +""" 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. @@ -44,4 +44,4 @@ def twoSum(self, nums: List[int], target: int) -> List[int]: -109 <= nums[i] <= 109 -109 <= target <= 109 Only one valid answer exists. -''' \ No newline at end of file +""" \ No newline at end of file From 790362d98549e2524857623310c44b0fe069ab54 Mon Sep 17 00:00:00 2001 From: Daniel Castro Date: Wed, 27 Oct 2021 14:06:56 +0100 Subject: [PATCH 3/3] Deleted problem description from solution --- LeetCode/Problems/Python/2_Two_Sum.py | 35 --------------------------- 1 file changed, 35 deletions(-) diff --git a/LeetCode/Problems/Python/2_Two_Sum.py b/LeetCode/Problems/Python/2_Two_Sum.py index 22be149f..3bbfc6b3 100644 --- a/LeetCode/Problems/Python/2_Two_Sum.py +++ b/LeetCode/Problems/Python/2_Two_Sum.py @@ -10,38 +10,3 @@ def twoSum(self, nums, target): # we do not need to return because we assume that # we have exactly one solution # return -1 - -""" -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] -Output: 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] - - - -Constraints: - - 2 <= nums.length <= 104 - -109 <= nums[i] <= 109 - -109 <= target <= 109 - Only one valid answer exists. -""" \ No newline at end of file