检查队列元素在Python中是否成对连续
假设我们有一个充满数字的队列。我们必须检查队列中的连续元素是否成对连续。
因此,如果输入类似于que=[3,4,6,7,8,9],则输出将为True。
示例
让我们看下面的实现以更好地理解-
import queue
def solve(que):
q = queue.Queue()
for i in que:
q.put(i)
temp = []
while q.qsize() != 0:
temp.append(q.queue[0])
q.get()
temp2 = []
while len(temp) != 0:
temp2.append(temp[len(temp) - 1])
temp.pop()
result = bool(True)
while len(temp2) > 1:
x = temp2[len(temp2) - 1]
temp2.pop()
y = temp2[len(temp2) - 1]
temp2.pop()
if abs(x - y) != 1:
result = False
q.put(x)
q.put(y)
if len(temp2) == 1:
q.put(temp2[len(temp2) - 1])
return result
que = [3,4,6,7,8,9]
print(solve(que))输入
[3,4,6,7,8,9]输出结果
True
热门推荐
10 祝女儿简短祝福语大全
11 大学新年祝福语简短创意
12 元旦适合的祝福语简短
13 朋友出远门祝福语简短
14 初六简短的祝福语
15 祝男孩生日祝福语简短
16 同事调离的祝福语简短
17 拜年红包的祝福语简短
18 妈妈生日祝福语简短励志