双指针问题
数组合并
合并有序数组(合并后的数组也应该是有序的)
#双指针问题之数组合并
arr1=[1,3,4,6,10]
arr2=[2,5,8,11]
ind=0
for num2 in arr2:
while ind <len(arr1):
if num2<=arr1[ind]:
arr1.insert(ind,num2)
break
else:
ind+=1
else:#arr2中剩余的数直接添加到arr1后面
arr1.append(num2)
print(arr1)
二分查找
二分查找法:折半查找,每次查找后查找范围折半。
#二分查找法
number=[1,3,5,6,7,8,13,14,15,17,18,24,30,43,56]
head,tail=0,len(number)-1
num=int(input())
while tail-head > 1:
if num==number[int(head)]:
print(head)
break
elif num==number[int(tail)]:
print(tail)
break
elif num>number[int((tail+head)/2)]:
head=int((tail+head)/2)
continue
elif num<number[int((tail+head)/2)]:
tail=int((tail+head)/2)
continue
else:
print(int((tail+head)/2))
break
else:
print('Error,数组中没有这个数')
链表
单链表
#链表
ListValue=[1,5,6,2,4,3]
ListPointer=[3,2,-1,5,1,4]
#模拟单链表1
##链表输出
print(ListValue[0])
next=ListPointer[0]
while next != -1:
print(ListValue[next])
next=ListPointer[next]#这边也要用next
#模拟单链表2
Linked_List=[[1,3],[5,2],[6,-1],[2,5],[4,1],[3,4]]
value,pointer=0,1
print(Linked_List[0][value])
next=Linked_List[0][1]
while next != -1:
print(Linked_List[next][value])
next=Linked_List[next][pointer]#这边也要用next
双链表构建及双向输出
#建立双向链表
List_Value = [1,5,6,2,7,3]
ListRight=[3,2,4,5,-1,1]
ListLeft=[-1,5,1,0,2,3]
right = 1
left = 2
value = 0
Linked_List=[[1,3,-1],[5,2,5],[6,4,1],[2,5,0],[7,-1,2],[3,1,3]]
#正序输出
##模拟双链表1
print(List_Value[0])
next=ListRight[0]
while next!=-1:
print(List_Value[next])
next=ListRight[next]
print('----------------------')
##模拟双链表2
next=0
while next!=-1:
print(Linked_List[next][value])
next=Linked_List[next][right]
print('----------------------')
#反向输出1
prior=ListRight.index(-1)
while prior != -1:
print(List_Value[prior])
prior=ListLeft[prior]
向单链表中添加元素
#像单链表中添加元素
ListValue=[1,5,6,2,7,3]
ListRight=[3,2,4,5,-1,1]
head=0
prepos=5
def Output(ListValue,ListRight,head):
print(ListValue[head])
next=ListRight[head]
while next!=-1:
print(ListValue[next])
next=ListRight[next]
#接下来添加元素
ListValue.append(4)
ListRight.append(ListRight[prepos])
ListRight[prepos]=len(ListValue)-1
Output(ListValue,ListRight,0)
向双链表中添加元素
#像双向链表中添加元素
def Output(ListValue,ListRight,ListLeft,head):
print(ListValue[head])
next=ListRight[head]
while next!=-1:
print(ListValue[next])
next=ListRight[next]
print('********************')
prior=ListRight.index(-1)
while prior != -1:
print(ListValue[prior])
prior=ListLeft[prior]
ListValue = [1,5,6,2,7,3]
ListRight=[3,2,4,5,-1,1]
ListLeft=[-1,5,1,0,2,3]
head=0
prepos=5
ListValue.append(4)
ListRight.append(ListRight[prepos])
ListLeft.append(prepos)
ListLeft[ListRight[prepos]]=len(ListValue)-1 #右边元素的左指针应该是4
ListRight[prepos]=len(ListValue)-1
单双链表的元素删除
#删除单链表中的元素
def Output1(ListValue,ListRight,head):
print(ListValue[head])
next=ListRight[head]
while next!=-1:
print(ListValue[next])
next=ListRight[next]
#删除单链中的元素"5":
ListValue = [1,5,6,2,7,3]
ListRight=[3,2,4,5,-1,1]
prepos=5
ListRight[prepos]=ListRight[ListRight[prepos]]
#删除双链表中的元素
def Output(ListValue,ListRight,ListLeft,head):
print(ListValue[head])
next=ListRight[head]
while next!=-1:
print(ListValue[next])
next=ListRight[next]
print('********************')
prior=ListRight.index(-1)
while prior != -1:
print(ListValue[prior])
prior=ListLeft[prior]
ListValue = [1,5,6,2,7,3]
ListRight=[3,2,4,5,-1,1]
ListLeft=[-1,5,1,0,2,3]
head=0
prepos=5
#删除元素“5”
ListRight[prepos]=ListRight[ListRight[prepos]]
ListLeft[ListRight[prepos]]=prepos
print(ListLeft)
Output(ListValue,ListRight,ListLeft,head)