Narcissistic Number
class Solution {
/*
* @param n: The number of digits.
* @return: All narcissistic numbers with n digits.
*/
public ArrayList<Integer> getNarcissisticNumbers(int n) {
// write your code here
ArrayList<Integer> narNum = new ArrayList<Integer>();
// consider 0
// Math.pow will return a double, not int
int start = (int) Math.pow(10, n-1);
if (n == 1) start = 0;
for (int i = start; i < (int) Math.pow(10, n); i++) {
int value = i;
int sum = 0;
while (value / 10 != 0 || value % 10 != 0) {
int digit = value % 10;
sum += Math.pow(digit, n);
value /= 10;
}
if (sum == i) {
narNum.add(sum);
}
}
return narNum;
}
};