2020.10.20每日复习
970.强整数在这里插入图片描述 **分析**
970.强整数
分析
先找到x和y的最小次幂,再在最小次幂里进行循环判断。用换底公式求最小次幂时,bound-1是因为要考虑另一个数的影响,另一个数至少为1,故最大只能到bound-1。
class Solution {
public List
<Integer> powerfulIntegers(int x
, int y
, int bound
) {
Set
<Integer> set
= new HashSet<>();
int xNum
= (x
== 1 ? 0 : (int)(Math
.log(bound
- 1) / Math
.log(x
));
int yNum
= (y
== 1 ? 0 : (int)(Math
.log(bound
- 1) / Math
.log(y
));
for(int i
= 0; i
<= xNum
; i
++) {
for(int j
= 0; j
<= yNum
; j
++) {
if(Math
.pow(x
, i
) + Math
.pow(y
, j
) <= bound
) {
set
.add((int)(Math
.pow(x
, i
) + Math
.pow(y
, j
)));
}else {
break;
}
}
}
return new ArrayList<>(set
);
}
}
转载请注明原文地址: https://lol.8miu.com/read-8152.html