首页 / PYTHON / Python小练习——双指针问题
Python小练习——双指针问题
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python小练习——双指针问题,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3680字,纯文字阅读大概需要6分钟。
内容图文
![Python小练习——双指针问题](/upload/InfoBanner/zyjiaocheng/731/ffc45f387dc645cd86f8c348eafb3bfd.jpg)
文章目录
由于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
内容总结
以上是互联网集市为您收集整理的Python小练习——双指针问题全部内容,希望文章能够帮你解决Python小练习——双指针问题所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。