双指针问题python描述

本文介绍了使用Python实现双指针解决算法问题的方法,包括合并有序数组、有序列表的二分查找、处理单链表和双链表等操作。通过实例代码详细解析了如何运用双指针技巧来操作数据结构。
摘要由CSDN通过智能技术生成

你也能看懂的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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值