Jun-22-2022, 05:59 PM
Hi
I am new to Python and I saw this two sum algorithm solution
In this line prevMap[i] = 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
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[i] = 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 ?