剑指offer,page39
# # 寻找数组中重复的数字
def bool_ifrepeat1 ( arr) :
for ii in range ( len ( arr) ) :
if arr[ ii] != ii:
arr[ ii] , arr[ arr[ ii] ] = arr[ arr[ ii] ] , arr[ ii]
def bool_ifrepeat2 ( arr) :
for ii in range ( len ( arr) ) :
print ( 111 )
while arr[ ii] != ii:
print ( 222 )
if arr[ ii] == arr[ arr[ ii] ] :
return True
# exchange the position
# arr[ ii] , arr[ arr[ ii] ] = arr[ arr[ ii] ] , arr[ ii] error!!
x1, x2 = arr[ arr[ ii] ] , arr[ ii]
arr[ arr[ ii] ] = x2
arr[ ii] = x1
print ( arr)
return False
def bool_ifrepeat3 ( arr) :
if len ( arr) <= 0 :
return False
for ii in range ( len ( arr) ) :
if arr[ ii] >= len ( arr) or arr[ ii] < 0 :
return False
for ii in range ( len ( arr) ) :
print ( 111 )
while arr[ ii] != ii:
print ( 222 )
if arr[ ii] == arr[ arr[ ii] ] :
return True
# exchange the position
# arr[ ii] , arr[ arr[ ii] ] = arr[ arr[ ii] ] , arr[ ii] error!!
x1, x2 = arr[ arr[ ii] ] , arr[ ii]
arr[ arr[ ii] ] = x2
arr[ ii] = x1
print ( arr)
return False
ans = bool_ifrepeat3 ( [ 2 , 3 , 1 , 0 , 2 , 5 , 3 ] )
print ( ans)