2020.10.20每日复习

it2023-08-16  65

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<>(); //若x或y为1,幂只能为0 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)));//Math.pow()返回的double类型数据,需强制类型转换成int类型 }else { break; } } } return new ArrayList<>(set); } }
最新回复(0)