简体   繁体   English

如何检查列表是否是python中的连续数字?

[英]how to check if a list is a consecutive number in python?

How to write python code that let the computer know if the list is a right sequence and the position doesn't matter, it will return true, otherwise it return false.如何编写python代码让计算机知道列表是否是正确的序列并且位置无关紧要,它将返回true,否则返回false。 below are some of my example, I really don't know how to start下面是我的一些例子,我真的不知道如何开始

b=[1,2,3,4,5] #return true
b=[1,2,2,1,3] # return false
b=[2,3,1,5,4] #return true
b=[2,4,6,4,3] # return false 

sort function is O(nlogn), we can use for loop which is O(n):排序函数是 O(nlogn),我们可以使用 O(n) 的 for 循环:

def check_seq(in_list):
    now_ele = set()
    min_ele = max_ele = in_list[0]

    for i in in_list:
        if i in now_ele:
            return False
        min_ele = min(i, min_ele)
        max_ele = max(i, max_ele)
        now_ele.add(i)
    
    if max_ele-min_ele+1 == len(in_list):
        return True
    return False

根据最小值和最大值创建一组和一个进行比较:

isRightSequence = set(range(min(b), max(b)+1)) == set(b)

This question is quite simple and can be solved a few ways.这个问题很简单,可以通过几种方式解决。

  1. The conditional approach - if there is a number that is bigger than the length of the list, it automatically cannot be a sequence because there can only be numbers from 1-n where n is the size of the list.条件方法 - 如果有一个大于列表长度的数字,它自动不能是一个序列,因为只能有 1-n 的数字,其中 n 是列表的大小。 Also, you have to check if there are any duplicates in the list as this cannot be possible either.此外,您必须检查列表中是否有任何重复项,因为这也不可能。 If none of these conditions occur, it should return true如果这些条件都没有发生,它应该返回 true

  2. Using dictionary - go through the entire list and add it as a key to a dictionary.使用字典 - 浏览整个列表并将其作为键添加到字典中。 Afterwards, simply loop through numbers 1-n where n is the length of the list and check if they are keys in the dictionary, if one of them isn't, return false.之后,只需遍历数字 1-n,其中 n 是列表的长度,并检查它们是否是字典中的键,如果其中一个不是,则返回 false。 If all of them are, return true.如果都是,则返回 true。

Both of these are quite simply approaches and you should be able to implement them yourselves.这两种方法都很简单,您应该能够自己实现它们。 However, this is one implementation for both.但是,这是两者的一种实现方式。

1. 1.

def solve(list1):
    seen = {}
    for i in list1:
        if i > len(list1):
            return False
        if i in seen:
            return False
        seen[i] = True
    return False
def solve(list1):
    seen = {}
    for i in list1:
        seen[i] = True
    for i in range (1, len(list1)+1):
        if i not in seen:
            return False
    return True

This solution needs O(n) runtime and O(n) space该解决方案需要 O(n) 运行时间和 O(n) 空间


def is_consecutive(l: list[int]):
    if not l: 
        return False
    low = min(l)
    high = max(l)
    # Bounds Check
    if high - low != len(l) - 1:
        return False
    
    # Test all indices exist
    test_vec = [False] * len(l) # O(n)
    for i in range(len(l)): 
        test_vec[l[i] - low] = True
    return all(test_vec)

assert is_consecutive(range(10))
assert is_consecutive([-1, 1,0])
assert not is_consecutive([1,1])
assert not is_consecutive([1,2,4,6,5])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM