I tried to solve an easy leecode question and I keep getting index out of bounds error, when I try the same solution on visual studio I dont get this error.
Here is the question and the code (I am failing a example 3):
Here is my code:
def merge(nums1, m, nums2, n ):
"""
Do not return anything, modify nums1 in-place instead.
"""
i1 = 0
i2 = 0
tmp = 0
while i1 < n + m :
if nums1[i1] == 0:
nums1[i1] = nums2[i2]
i1+=1
i2+=1
elif nums1[i1] <= nums2[i2]:
i1 += 1
else:
tmp = nums2[i2]
nums2[i2] = nums1[i1]
nums1[i1] = tmp
i1 += 1
It says Im out of bounds on this line: nelif nums1[i1] <= nums2[i2]:
But I never reach this line because at this point I am out of the loop.
The error:
Runtime Error
IndexError: list index out of range
~~~~~^^^^
elif nums1[i1] <= nums2[i2]:
Line 16 in merge (Solution.py)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ret = Solution().merge(param_1, param_2, param_3, param_4)
Line 58 in _driver (Solution.py)
_driver()
Line 70 in <module> (Solution.py)