How to see if the list contains consecutive numbers

后端 未结 12 1369
别跟我提以往
别跟我提以往 2021-02-07 12:50

I want to test if a list contains consecutive integers and no repetition of numbers. For example, if I have

l = [1, 3, 5, 2, 4, 6]

It should re

12条回答
  •  梦毁少年i
    2021-02-07 13:25

    def solution(A):
        counter = [0]*len(A)
        limit = len(A)
        for element in A:
            if not 1 <= element <= limit:
                return False
            else:
                if counter[element-1] != 0:
                    return False
                else:
                    counter[element-1] = 1
    
        return True
    

提交回复
热议问题