967. 连续差相同的数字

在这里插入图片描述

思路:

N位数,可以先找到满足条件的一位数、二位数、三位数。。。直到N维数。
class Solution(object):
    def numsSameConsecDiff(self, N, K):
        """
        :type N: int
        :type K: int
        :rtype: List[int]
        """
        if N == 1:
            return list(range(10))
        
        res = list(range(1, 10))
        for _ in range(1, N):
            tmp_res = list()
            for num in res:
                num_s = num % 10
                for i in {num_s - K, num_s + K}:
                    if 0 <= i <= 9:
                        tmp_res.append(num * 10 + i)
            res = tmp_res
        return res

参考博客:https://blog.csdn.net/qq_17550379/article/details/85852347

猜你喜欢

转载自blog.csdn.net/zyy848877920/article/details/88906909