+ 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]
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
+ 1
s = sum?
0
My code doesn't pass "Submit" on leetcode.com problems. I guess this code doesn't work in all cases.