Подскажите, пожалуйста, добрые джаварашники, что не так в моем решении. Даже если буду чистить буффер и uniqueNumbers, чтобы делать все с самого начала, то не проходит по последнему пункту
package com.javarush.task.task20.task2025;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
/*
Алгоритмы-числа
*/
public class Solution {
private static final HashMap<Long, HashMap<Integer, Long>> exponents = new HashMap<>();
private static final HashSet<String> uniqueNumbers = new HashSet<>();
private static final ArrayList<Long> buffer = new ArrayList<>();
static {
exponents.put(0L, new HashMap<>());
exponents.put(1L, new HashMap<>());
exponents.put(2L, new HashMap<>());
exponents.put(3L, new HashMap<>());
exponents.put(4L, new HashMap<>());
exponents.put(5L, new HashMap<>());
exponents.put(6L, new HashMap<>());
exponents.put(7L, new HashMap<>());
exponents.put(8L, new HashMap<>());
exponents.put(9L, new HashMap<>());
exponents.get(0L).put(1, 0L);
exponents.get(1L).put(1, 1L);
exponents.get(2L).put(1, 2L);
exponents.get(3L).put(1, 3L);
exponents.get(4L).put(1, 4L);
exponents.get(5L).put(1, 5L);
exponents.get(6L).put(1, 6L);
exponents.get(7L).put(1, 7L);
exponents.get(8L).put(1, 8L);
exponents.get(9L).put(1, 9L);
}
public static long[] getNumbers(long N) {
long i = 1L;
if (!buffer.isEmpty()) {
i = buffer.stream().mapToLong(w -> w).max().getAsLong();
if (i > N) return buffer.stream().filter(w -> w <= N).mapToLong(w -> w).sorted().toArray();
}
while (i < N) {
check(i);
i++;
}
return buffer.stream().mapToLong(w -> w).sorted().toArray();
}
private static long sum(long num) {
long addition = 0;
int len = ("" + num).length();
long lastDigit = num % 10;
while (num >= 1) {
addition += pow(lastDigit, len);
num /= 10;
lastDigit = num % 10;
}
return addition;
}
private static void check(long num) {
String stringVis = sortString(num);
long total;
if (uniqueNumbers.contains(stringVis)) return;
total = sum(num);
if (sortString(total).equals(stringVis)) buffer.add(total);
uniqueNumbers.add(stringVis);
}
private static String sortString(long num) {
char[] array = Long.toString(num).toCharArray();
Arrays.sort(array);
return new String(array);
}
private static long pow(long digit, int exponent) {
Long res = exponents.get(digit).getOrDefault(exponent, null);
if (res == null) {
exponents.get(digit).put(exponent, exponents.get(digit).getOrDefault(exponent, pow(digit, exponent - 1) * digit));
}
return exponents.get(digit).get(exponent);
}
public static void main(String[] args) {
long a = System.currentTimeMillis();
System.out.println(Arrays.toString(getNumbers(100000000)));
long b = System.currentTimeMillis();
System.out.println("memory " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / (8 * 1024));
System.out.println("time = " + (b - a));
a = System.currentTimeMillis();
System.out.println(Arrays.toString(getNumbers(1000000)));
b = System.currentTimeMillis();
System.out.println("memory " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / (8 * 1024));
System.out.println("time = " + (b - a));
}
}