交换数组中的元素导致问题
swap elements in an array causing issue
新年快乐。我有一个关于在 Python 环境下交换数组中的元素的问题。代码 w/o 问题是:
def findMissingNumber(nums):
length = len(nums)
result = []
i = 0
while i < length:
# num = nums[i]
target = nums[i] - 1
if nums[i] != i+1 and nums[i] != nums[target]:
# swap with the nums[i]-1
nums[target], nums[i]= nums[i], nums[target]
else:
i += 1
for j in range(length):
if nums[j] != j+1:
result.append(j+1)
return result
print(findMissingNumber([2, 3, 1, 8, 2, 3, 5, 1]))
print(findMissingNumber([2, 4, 1, 2]))
print(findMissingNumber([2, 3, 2, 1]))
答案:
[4, 6, 7]
[3]
[4]
错误的是:
def findMissingNumber(nums):
length = len(nums)
result = []
i = 0
while i < length:
num = nums[i]
target = num - 1
if num != i+1 and num != nums[target]:
# swap with the num-1
nums[target], num = num, nums[target]
else:
i += 1
for j in range(length):
if nums[j] != j+1:
result.append(j+1)
return result
print(findMissingNumber([2, 3, 1, 8, 2, 3, 5, 1]))
print(findMissingNumber([2, 4, 1, 2]))
print(findMissingNumber([2, 3, 2, 1]))
答案:
[4, 6, 7]
[3, 4]
[3, 4]
我不确定为什么引用(我应该这样称呼)造成麻烦。你介意详细解释一下吗?
谢谢
修复了带有注释的代码。
def findMissingNumber(nums):
length = len(nums)
result = []
i = 0
while i < length:
# num and nums[i] have the same value but doesnt refer to same memory location
num = nums[i]
target = num - 1
if num != i+1 and num != nums[target]:
# swap with the num-1
# incorrect swap, num value is not referencing the list index, but has a copy of its value,
# so instead of swapping value with num, do it from the list like below
# nums[target], num = num, nums[target]
# correct swap
nums[target], nums[i] = nums[i], nums[target]
else:
i += 1
for j in range(length):
if nums[j] != j+1:
result.append(j+1)
return result
print(findMissingNumber([2, 3, 1, 8, 2, 3, 5, 1]))
print(findMissingNumber([2, 4, 1, 2]))
print(findMissingNumber([2, 3, 2, 1]))
新年快乐。我有一个关于在 Python 环境下交换数组中的元素的问题。代码 w/o 问题是:
def findMissingNumber(nums):
length = len(nums)
result = []
i = 0
while i < length:
# num = nums[i]
target = nums[i] - 1
if nums[i] != i+1 and nums[i] != nums[target]:
# swap with the nums[i]-1
nums[target], nums[i]= nums[i], nums[target]
else:
i += 1
for j in range(length):
if nums[j] != j+1:
result.append(j+1)
return result
print(findMissingNumber([2, 3, 1, 8, 2, 3, 5, 1]))
print(findMissingNumber([2, 4, 1, 2]))
print(findMissingNumber([2, 3, 2, 1]))
答案:
[4, 6, 7]
[3]
[4]
错误的是:
def findMissingNumber(nums):
length = len(nums)
result = []
i = 0
while i < length:
num = nums[i]
target = num - 1
if num != i+1 and num != nums[target]:
# swap with the num-1
nums[target], num = num, nums[target]
else:
i += 1
for j in range(length):
if nums[j] != j+1:
result.append(j+1)
return result
print(findMissingNumber([2, 3, 1, 8, 2, 3, 5, 1]))
print(findMissingNumber([2, 4, 1, 2]))
print(findMissingNumber([2, 3, 2, 1]))
答案:
[4, 6, 7]
[3, 4]
[3, 4]
我不确定为什么引用(我应该这样称呼)造成麻烦。你介意详细解释一下吗?
谢谢
修复了带有注释的代码。
def findMissingNumber(nums):
length = len(nums)
result = []
i = 0
while i < length:
# num and nums[i] have the same value but doesnt refer to same memory location
num = nums[i]
target = num - 1
if num != i+1 and num != nums[target]:
# swap with the num-1
# incorrect swap, num value is not referencing the list index, but has a copy of its value,
# so instead of swapping value with num, do it from the list like below
# nums[target], num = num, nums[target]
# correct swap
nums[target], nums[i] = nums[i], nums[target]
else:
i += 1
for j in range(length):
if nums[j] != j+1:
result.append(j+1)
return result
print(findMissingNumber([2, 3, 1, 8, 2, 3, 5, 1]))
print(findMissingNumber([2, 4, 1, 2]))
print(findMissingNumber([2, 3, 2, 1]))