Powerful Integers
给一个边界bound和 x^i + y^j 公式, 算bound内的所有values. 就是按照题意做, 然后注意一下两个数等于1的时候的特殊情况(即使break,免得increment不更新而死循环).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
class Solution { public List<Integer> powerfulIntegers(int x, int y, int bound) { List<Integer> res = new ArrayList<>(); Set<Integer> set = new HashSet<>(); if(bound < 2) return res; for(int i = 1; i < bound; i*=x) { for(int j = 1; j < bound; j*=y) { if(i + j <= bound) { System.out.println(i+"a"+j); set.add(i+j); } if(y == 1) // if y == 1, then j is 1 always, then there is no reason to continue break; } if(x == 1) // if x == 1, then i is 1 always, then there is no reason to continue break; } res.addAll(set); return res; } } |