Python小练习——双指针问题

文章目录

    • 1. 有序数组合并
    • 2. 二分查找
    • 3. 单链表
      • 3.1 构建单链表
      • 3.2 向单链表添加元素
      • 3.3 删除单链表元素
    • 4. 双链表
      • 4.1 正序输出
      • 4.2 双向输出
      • 4.3 向双链表添加元素
      • 4.4 删除双链表中元素

由于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

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

你可能感兴趣的:(算法,数据结构)