给出两个从小到大的有序数组,将两个数组合并成一个新的从小到大的有序数组
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
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)
# 方案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]
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)
# 省略建立单链表部分
right[pre] = right[right[pre]]
outLs(value, right, head)
# 方案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]
# 方案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]
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)
right[pre] = right[right[pre]]
left[right[right[pre]]] = pre