Python小练习——双指针问题


由于Python中没有指针的概念,这里只是用数组模拟指针的方式。

1. 有序数组合并

给出两个从小到大的有序数组,将两个数组合并成一个新的从小到大的有序数组

ls1 = list(map(int,input(‘输入第一个数组’).split()))
ls2 = list(map(int,input(‘输入第二个数组’).split()))
index = 0
ans = ls1.copy()
for i in range(0, len(ls2)):
    while index<len(ls1):
        if ls2[i]<=ls1[index]:
            ans.insert(index+i, ls2[i])
            break
        else:
            index += 1
    else:
        # ans = ans + ls2[i:]
        ans.insert(index+i, ls2[i])
print(ans)

2. 二分查找

num = list(map(int, input(‘输入待查数组:’).split()))
search = int(input(‘输入要查找的数字:’))
head, tail = 0, len(num)
while tail - head > 1:
    mid = (tail+head)//2
    if search < num[mid]:
        tail = mid
    elif search > num[mid]:
        head = mid + 1
    elif search == num[mid]:
        ans = mid
        break
else:
    if search == num[head]:
        ans = mid
    else:
        ans = -1
print(ans)

3. 单链表

3.1 构建单链表

# 方案1
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]

# 方案2
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]

3.2 向单链表添加元素

def outLs(value, right, head):
    print(value[head])
    next = right[head]
    while next != -1:
        print(value[next])
        next = right[next]
value = [1, 5, 6, 2, 7, 3]
right = [3, 2, 4, 5, -1, 1]
head = 0
pre = 5

value.append(4)
right.append(right[pre])
right[pre] = len(value) - 1
outLs(value, right, head)

3.3 删除单链表元素

# 省略建立单链表部分
right[pre] = right[right[pre]]
outLs(value, right, head)

4. 双链表

4.1 正序输出

# 方案1
value = [1, 5, 6, 2, 7, 3]
right = [3, 2, 4, 5, -1, 1]
left = [-1, 5, 1, 0, 2, 3]

head = left.index(-1)
print(value[head])
Next = right[head]

while Next > -1:
    print(value[Next])
    Next = right[Next]
# 方案2
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]

4.2 双向输出

# 方案1
value = [1, 5, 6, 2, 7, 3]
right = [3, 2, 4, 5, -1, 1]
left = [-1, 5, 1, 0, 2, 3]

head = left.index(-1)
print(value[head])
Next = right[head]

while Next > -1:
    print(value[Next])
    Next = right[Next]

head = right.index(-1)
print(value[head])
Next = left[head]

while Next > -1:
    print(value[Next])
    Next = left[Next]

# 方案2
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]

head = 4
print(LinkedList[head][value])
Next = LinkedList[head][left]
while Next > -1:
    print(LinkedList[Next][value])
    Next = LinkedList[Next][left]

4.3 向双链表添加元素

def outLs(value, right, head):
    head = left.index(-1)
    print(value[head])
    Next = right[head]

    while Next > -1:
        print(value[Next])
        Next = right[Next]

    head = right.index(-1)
    print(value[head])
    Next = left[head]

    while Next > -1:
        print(value[Next])
        Next = left[Next]

        
value = [1, 5, 6, 2, 7, 3]
right = [3, 2, 4, 5, -1, 1]
left = [-1, 5, 1, 0, 2, 3]
head = 0 #头指针
pre = 5 #前一个元素位置

value.append(4)
right.append(right[pre])
left.append(pre)
left[right[pre]] = len(value) - 1
right[pre] = len(value) - 1
outLs(value, right, head)

4.4 删除双链表中元素

right[pre] = right[right[pre]]
left[right[right[pre]]] = pre
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Growing_Snake

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值