Can you correct my code, please? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can you correct my code, please?

#two sum problem from leetcode.com #Description: Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same element twice. Example: Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1]. #my code: def twoSum(self, nums: List[int], target: int) -> List[int]: l = len(nums) for i in range(l): s = sum for j in range(l): s = sum([nums[i], nums[j]]) if s == target and i != j: return [j, i]

26th Jan 2020, 8:41 PM
Andrey Shintar
Andrey Shintar - avatar
3 Answers
+ 1
Your code returns indecies, not values. Fixed that. Also made sure that you get an input from server or user. Not sure if that was part of a problem. Other than that, the overall logic looks fine. Here is a modified code: https://code.sololearn.com/cFMz4mmaVsu9/#py
26th Jan 2020, 8:59 PM
Khatakh
Khatakh - avatar
+ 1
s = sum?
26th Jan 2020, 9:03 PM
Oma Falk
Oma Falk - avatar
0
My code doesn't pass "Submit" on leetcode.com problems. I guess this code doesn't work in all cases.
26th Jan 2020, 8:44 PM
Andrey Shintar
Andrey Shintar - avatar