The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
You may assume that each input would have exactly one solution.
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
Output: index1=1, index2=2
Solution:
class Solution: # @return a tuple, (index1, index2) def twoSum(self, num, target): processed = {} for i in range(0, len(num)): if target-num[i] in processed: return [processed[target-num[i]]+1,i+1] processed[num[i]]=i
How about below input?
ReplyDeletenums=[3,2,4,4,3,2,5,1]
4+5 = 9, there are 2 fours, so that input doesn't fit the question.
Deletehello, your website is really good. We do appreciate your give good results https://python.engineering/python-add-comma-between-numbers/
ReplyDelete