1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| class Solution { public int distinctIntegers(int n) { Set<Integer> set = new HashSet<>(); Queue<Integer> queue = new LinkedList<>(); queue.offer(n); while (!queue.isEmpty()) { int size = queue.size(); for (int k = 0; k < size; ++k) { int x = queue.poll(); for (int i = 1; i <= n; ++i) { if (!set.contains(i) && x % i == 1) { queue.offer(i); set.add(i); } } } } return set.size() + 1; } }
|