题目描述:
代码:
class Solution(object):
#使用双指针
def exchange(self, nums):
head,tail=0,len(nums)-1
#如果头指针指向偶数,尾指针指向奇数,则交换数字
while head<=tail:
if nums[head]%2==0 and nums[tail]%2==1:
temp=nums[head]
nums[head]=nums[tail]
nums[tail]=temp
if nums[head]%2==1:
head+=1
if nums[tail]%2==0:
tail-=1
return nums