标签:odi color 数组 author span bsp == ret 交换
#encoding:utf-8
_author_ = "Wang Wenchao"
#荷兰国旗问题 0放在左边,1放在中间,2放在右边
arr=[2,2,2,2,1,2,0,2,0,1,0]
#{}1,2,0,2,0,1,0{}
#用i遍历数组,是0区就和零区下一个交换,是2区就和2区前一个交换
def OneZeroTwo(arr):
length=len(arr)
if length==1 or not arr:
return arr
low=-1
high=length
i=0
while i<high:
if arr[i]==1:
i+=1
elif arr[i]==0:
if i==low+1:
low+=1
i+=1
else:
arr[i],arr[low+1]=arr[low+1],arr[i]
low+=1
else:
if i+1==high:
high-=1
i+=1#break
else:
arr[i],arr[high-1]=arr[high-1],arr[i]
high-=1
return arr
print OneZeroTwo(arr)
标签:odi color 数组 author span bsp == ret 交换
原文地址:http://www.cnblogs.com/BetterThanEver-WWCH/p/7534780.html