william2341
New Coder
Hi
I am new to Python and I saw this two sum algorithm solution
In this line prevMap[inedx] = n I should add a value at index but it return empty array [] or should I call it map or list ? but if I use it like this prevMap[n] = i it work correctly I don understant why ? I am comming from other languages where I store new value into array like this myarray[index] = value; so python is do it in the opposit way ?
I am new to Python and I saw this two sum algorithm solution
Python:
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
prevMap = {}
for i, n in enumerate(nums):
diff = target - n
if diff in prevMap:
return [prevMap[diff], i]
prevMap[i] = n
In this line prevMap[inedx] = n I should add a value at index but it return empty array [] or should I call it map or list ? but if I use it like this prevMap[n] = i it work correctly I don understant why ? I am comming from other languages where I store new value into array like this myarray[index] = value; so python is do it in the opposit way ?