Lintcode 147. 水仙花数

Lintcode 147. 水仙花数

Lintcode 147. 水仙花数

解答:

public List<Integer> getNarcissisticNumbers(int n){
    List<Integer> nums = new ArrayList<>();
    int start = 1;
    for (int i = 0;i<n - 1;i++){
        start *= 10;
    }
    int  end = start * 10;
    if (start==1){
        nums.add(0);
    }
    for (int i = start;i<end;i++){
        Integer integer = JudgeNum(i, n);
        if (null != integer){
            nums.add(integer);
        }
    }
    return nums;
}

public static Integer JudgeNum(int num,int n){
    int count = 0;
    int ax = 1;
    int temp;
    int tempcount;
    for (int i=0;i<n;i++){
        temp  = num/ax%10;
        tempcount = temp;
        for (int j = 0;j<n-1;j++){
            tempcount = tempcount * temp;
        }
        count += tempcount;
        ax = ax * 10;
    }
    if (count == num){
        return num;
    }
    return null;
}