I'm current working on missing number, and I ran into a strange error.
def find_missing_number(nums):
i = 0
while i < len(nums):
#print(nums)
if nums[i] < len(nums) and nums[i] != nums[nums[i]]:
nums[nums[i]], nums[i] = nums[i], nums[nums[i]]
#nums[i], nums[nums[i]] = nums[nums[i]], nums[i] # THIS ONE GIVES AN ERROR?????
# temp = nums[nums[i]]
# nums[nums[i]] = nums[i]
# nums[i] = temp
else:
i+=1
I keep getting a list assignment index out of range for the line that I indicated gives an error.
Does anyone know why? I have an if statement making sure it won't access any values out of range.
Edit: I'm trying to the the Missing Number problem on Leetcode and use cyclic sort to sort nums. These are the test cases:
Input: [4, 0, 3, 1] Output: 2
Input: [8, 3, 5, 2, 4, 6, 0, 1] Output: 7
Thanks!