【每日一题1013】完美列表

:mage:t2:一个完美列表是指,在这个列表中的每个数字n,其n-1或者n+1也存在于列表中。请编写一个函数,判断给定的列表是否是完美列表。

示例:
输入:[2, 10, 9, 3],输出:True
解析:因为2=3-1,10=9+1,9=10-1,3=2+1,因此它是完美列表。

题目难度:简单
题目来源:codewars : Nice Array

def is_nice(nums: list) -> bool:
    pass
assert is_nice([2, 10, 9, 3]) is True
assert is_nice([3, 4, 5, 7]) is False
def is_nice(nums: list) -> bool:
    nums.sort()
    for i in range(len(nums)):
        min_index = i - 1 if i > 0 else i
        next_index = i + 1 if i < len(nums) - 1 else i
        min_num, num, next_num = nums[min_index], nums[i], nums[next_index]
        if not (min_num + 1 == num or num + 1 == next_num):
            return False
    return True
1 个赞
def is_nice(nums: list) -> bool:
    for n in nums:
        if (n + 1 in nums) or (n - 1 in nums):
            ...
        else:
            return False
    return True


assert is_nice([2, 10, 9, 3]) is True
assert is_nice([3, 4, 5, 7]) is False
def is_nice(nums: list) -> bool:
    # 遍历列表中的每个数字
    for num in nums:
        if num - 1 in nums:
            # 数字-1 的结果存在列表中则通过进入下一轮
            pass
        elif num + 1 in nums:
            # 数字+1 的结果存在列表中则通过进入下一轮
            pass
        else:
            # 否则退出函数,直接返回 False
            return False
    # 列表中所有数字都符合则为完美列表,返回 True
    return True


assert is_nice([2, 10, 9, 3]) is True
assert is_nice([3, 4, 5, 7]) is False
1 个赞
def is_nice(nums):
    count = 0
    for num in nums:
        num1 = num - 1
        num2 = num + 1
        if num1 in nums or num2 in nums:
            count += 1
            continue
        else:
            break
    if count == len(nums):
        return True
    return False


assert is_nice([2,10,9,3]) is True
assert is_nice([3,4,5,7]) is False

why?这样复杂度不是更高了吗?

的确复杂了,你这样可以简便好多

def is_nice(arg):
    for i in arg:
        if (i - 1 in arg) or (i + 1 in arg):
            pass
        else:
            return False
    return True


assert is_nice([2, 10, 9, 3]) is True
assert is_nice([3, 4, 5, 7]) is False

参考题解:

def is_nice(nums: list) -> bool:
    return all([i+1 in nums or i-1 in nums for i in nums])

assert is_nice([2, 10, 9, 3]) is True
assert is_nice([3, 4, 5, 7]) is False

思路:使用列表推导式获取布尔结果,然后判断当所有结果为真时返回True。

3 个赞
def is_nice(nums: list) -> bool:
    for item in nums:
        if (item -1) in nums or (item +1) in nums:
            flag = 1
        else:
            flag = 0
    return True if flag else False

assert is_nice([2, 10, 9, 3]) is True
assert is_nice([3, 4, 5, 7]) is False
def is_nice(nums: list) -> bool:
    for i in nums:
        a=int(i)-1
        b=int(i)+1
        if a in nums or b in nums:
            pass
        else:
            return False
    return True


assert is_nice([2, 10, 9, 3]) is True
assert is_nice([3, 4, 5, 7]) is False
def is_nice(nums: list) -> bool:
    for n in nums:
        nums2 = [x for x in nums]
        nums2.remove(n)
        for x in nums2:
            if n == x + 1 or n == x - 1:
                break
        else:
            return False
    # 列表中所有数字都符合则为完美列表,返回 True
    return True

assert is_nice([2, 10, 9, 3]) is True
assert is_nice([3, 4, 5, 7]) is False
def is_nice(nums: list) -> bool:
    for num in nums:
        if num + 1 not in nums and num - 1 not in nums:
            return False

    return True


is_nice([3, 4, 5, 7])
assert is_nice([2, 10, 9, 3]) is True
assert is_nice([3, 4, 5, 7]) is False
def is_nice(nums: list) -> bool:
    x = True
    for i in nums:
        if i+1 in nums or i-1 in nums:
            x=True
        else:
            x=False
    return x
assert is_nice([2, 10, 9, 3]) is True
assert is_nice([3, 4, 5, 7]) is False