双指针问题

你也能看懂的python算法书 代码

  1. 指针合并有序数组
#-*- coding:utf-8 -*-
arr1 = [1,3,4,6,10]
arr2=[2,3,6,8]
ind = 0
ans =arr1.copy()
for i in range(0,len(arr2)):
    while ind <len(arr1):
        if arr2[i]<=arr1[ind]:
            ans.insert(ind+i,arr2[i])
            break
        else:
            ind += 1
    else:
        ans = ans + arr2[i:]
print(ans)

  1. 指针实现有序列表二分查找
#-*- coding:utf-8 -*-
numbers = [1,4,5,6,7,9,12,15,19,34,46,60,74,190]
head ,tail =0, len(numbers)
search = int(input("Enter an number to search:"))
while tail - head >1:
    mid = (head+tail)//2
    if search>numbers[mid]:
        head = mid+1
    elif search<numbers[mid]:
        tail = mid
    else:
        print(mid)
        break
else:
    if search == numbers[head]:
        print(head)
    else:
        print(-1)

  1. 输出一个由两个列表组成的单链表
#-*- coding:utf-8 -*-
ListValue = [1,5,6,2,4,3]
ListPointer = [3,2,-1,5,1,4]
head = 0
print(ListValue[head])
next = ListPointer[head]
while next != -1:
    print(ListValue[next])
    next = ListPointer[next]

  1. 输出一个列表套列表组成的单链表
#-*- coding:utf-8 -*-
value = 0
pointer = 1
LinkedList =[[1,3],[5,2],[6,-1],[2,5],[4,1],[3,4]]
head =0
print(LinkedList[head][value])
next =LinkedList[head][pointer]
while next!= -1:
    print(LinkedList[next][value])
    next = LinkedList[next][pointer]
  1. 正序输出双链表(分开的多个数组模拟)
#-*- coding:utf-8 -*-
ListValue = [1,5,6,2,7,3]
ListRight = [3,2,4,5,-1,1]
ListLeft =[-1,5,1,0,2,3]
head = ListLeft.index(-1)
print(ListValue[head])
Next = ListRight[head]
while Next > -1:
    print(ListValue[Next])
    Next = ListRight[Next]

  1. 正序输出双链表(数组嵌套数组模拟)
right = 1
left = 2
value = 0
LinkedList =[[1,3,-1],[5,2,5],[6,4,1],[2,5,0],[7,-1,2],[3,1,3]]
head =0
print(LinkedList[head][value])
next = LinkedList[head][right]
while next >-1:
    print(LinkedList[next][value])
    next = LinkedList[next][right]
  1. 双向输出双链表
print("正向")
ListValue = [1,5,6,2,7,3]
ListRight = [3,2,4,5,-1,1]
ListLeft = [-1,5,1,0,2,3]

head = ListLeft.index(-1)
print(ListValue[head])
next = ListRight[head]
while next >-1:
    print(ListValue[next])
    next = ListRight[next]
print("反向")
head = ListRight.index(-1)
print(ListValue[head])
next = ListLeft[head]
while next > -1:
    print(ListValue[next])
    next = ListLeft[next]

  1. 单链表插入元素
def Output(ListValue,ListRight,head):
    print(ListValue[head])
    next = ListRight[head]
    while next != -1:
        print(ListValue[next])
        next = ListRight[next]
ListValue = [1,5,6,2,7,3]
ListRight = [3,2,4,5,-1,1]
head = 0
prepos = 5

Output(ListValue,ListRight,head)
print()
ListValue.append(4)
ListRight.append(ListRight[prepos])
ListRight[prepos] = len(ListValue)-1
Output(ListValue,ListRight,head)

  1. 双链表插入元素
def Output(ListValue,ListRight,head):
    print(ListValue[head])
    next = ListRight[head]
    while next!= -1:
        print(ListValue[next])
        next = ListRight[next]

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
Output(ListValue,ListRight,head)
print()

ListValue.append(4)
ListRight.append(ListRight[prepos])
ListLeft.append(prepos)
ListLeft[ListRight[prepos]] = len(ListValue) - 1
ListRight[prepos] = len(ListValue) - 1
Output(ListValue,ListRight,head)

  1. 删除单指针元素
def Output(ListValue,ListRight,head):
    print(ListValue[head])
    n = ListRight[head]
    while n != -1:
        print(ListValue[n])
        n = ListRight[n]
ListValue = [1,5,6,2,7,3]
ListRight = [3,2,4,5,-1,1]
head = 0 
prepos = 5
Output(ListValue,ListRight,head)
print()
ListRight[prepos] = ListRight[ListRight[prepos]]
Output(ListValue,ListRight,head)
  1. 删除双指针元素
def Output(ListValue,ListRight,head):
    print(ListValue[head])
    n = ListRight[head]
    while n != -1:
        print(ListValue[n])
        n = ListRight[n]
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
Output(ListValue,ListRight,head)
print()
ListRight[prepos] = ListRight[ListRight[prepos]]
ListLeft[ListRight[ListRight[prepos]]] = prepos
Output(ListValue,ListRight,head)

发布了24 篇原创文章 · 获赞 6 · 访问量 1351

猜你喜欢

转载自blog.csdn.net/Alexhcf/article/details/103562426
今日推荐