0
点赞
收藏
分享

微信扫一扫

python刷算法 LintCode 147 水仙花数

云竹文斋 2022-01-16 阅读 48
python算法

水仙花数的定义是,这个数等于他每一位数上的幂次之和 见维基百科的定义

比如一个3位的十进制整数153就是一个水仙花数。因为 153 = 13 + 53 + 33。

而一个4位的十进制数1634也是一个水仙花数,因为 1634 = 14 + 64 + 34 + 44。

给出n,找到所有的n位十进制水仙花数。

样例 1:
	输入: 1
	输出: [0,1,2,3,4,5,6,7,8,9]

样例 2:
输入:  2
输出: []	
样例解释: 没有2位数的水仙花数。
class Solution:
    """
    @param n: The number of digits
    @return: All narcissistic numbers with n digits
    """
    def getNarcissisticNumbers(self, n):
        # write your code here
        res = []
        for x in range([0, 10 ** (n - 1)][n > 1], 10 ** n):
            y, k = x, 0
            while x > 0:
                k += (x % 10) ** n
                x //= 10
            if k == y:
                res.append(k)
        return res
举报

相关推荐

计算“水仙花数”

求水仙花数

水仙花数(java)

打印水仙花数

JAVA——水仙花数

c水仙花数

0 条评论