147. 水仙花数

147. 水仙花数

中文English

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

比如一个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位数的水仙花数。

注意事项

你可以认为n小于8。

 
 
输入测试数据 (每行一个参数)如何理解测试数据?
class Solution:
    """
    @param n: The number of digits
    @return: All narcissistic numbers with n digits
    """
    def getNarcissisticNumbers(self, n):
        # write your code here
        res = []
        if n == 1:
            res = [0]
        start =10**(n-1)
        end = 10**n
        for s in range(start,end):
            c = 0
            for i in str(s):
                c += int(i)**n
            if c == s:
                res.append(c)
        return res

 

posted @ 2020-05-23 14:52  风不再来  阅读(313)  评论(0编辑  收藏  举报