
题目描述
给定三个整数 x
、 y
和 bound
,返回 值小于或等于 bound
的所有 强整数 组成的列表 。
如果某一整数可以表示为 xi + yj
,其中整数 i >= 0
且 j >= 0
,那么我们认为该整数是一个 强整数 。
你可以按 任何顺序 返回答案。在你的回答中,每个值 最多 出现一次。
示例 1:
输入:x = 2, y = 3, bound = 10
输出:[2,3,4,5,7,9,10]
解释:
2 = 20 + 30
3 = 21 + 30
4 = 20 + 31
5 = 21 + 31
7 = 22 + 31
9 = 23 + 30
10 = 20 + 32
示例 2:
输入:x = 3, y = 5, bound = 15
输出:[2,4,6,8,10,14]
提示:
1 <= x, y <= 100
0 <= bound <= 106
解法
方法一:哈希表 + 枚举
根据题目描述,一个强整数可以表示成 \(x^i + y^j\),其中 \(i \geq 0\), \(j \geq 0\)。
题目需要我们找出所有不超过 \(bound\) 的强整数,我们注意到 \(bound\) 的取值范围不超过 \(10^6\),而 \(2^{20} = 1048576 \gt 10^6\)。因此,如果 \(x \geq 2\),那么 \(i\) 最大不超过 \(20\),才有可能使得 \(x^i + y^j \leq bound\) 成立。同理,如果 \(y \geq 2\),那么 \(j\) 最大不超过 \(20\)。
因此我们可以使用双重循环,枚举所有可能的 \(x^i\) 和 \(y^j\),分别记为 \(a\) 和 \(b\),并保证 \(a + b \leq bound\),此时 \(a + b\) 即为一个强整数。我们使用哈希表存储所有满足条件的强整数,最后将哈希表中的所有元素转换成答案列表返回即可。
注意,如果 \(x=1\) 或者 \(y=1\),那么 \(a\) 或者 \(b\) 的值恒等于 \(1\),对应的循环只需要执行一次即可退出。
时间复杂度 \(O(\log^2 bound)\),空间复杂度 \(O(\log^2 bound)\)。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 | class Solution:
def powerfulIntegers(self, x: int, y: int, bound: int) -> List[int]:
ans = set()
a = 1
while a <= bound:
b = 1
while a + b <= bound:
ans.add(a + b)
b *= y
if y == 1:
break
if x == 1:
break
a *= x
return list(ans)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 | class Solution {
public List<Integer> powerfulIntegers(int x, int y, int bound) {
Set<Integer> ans = new HashSet<>();
for (int a = 1; a <= bound; a *= x) {
for (int b = 1; a + b <= bound; b *= y) {
ans.add(a + b);
if (y == 1) {
break;
}
}
if (x == 1) {
break;
}
}
return new ArrayList<>(ans);
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 | class Solution {
public:
vector<int> powerfulIntegers(int x, int y, int bound) {
unordered_set<int> ans;
for (int a = 1; a <= bound; a *= x) {
for (int b = 1; a + b <= bound; b *= y) {
ans.insert(a + b);
if (y == 1) {
break;
}
}
if (x == 1) {
break;
}
}
return vector<int>(ans.begin(), ans.end());
}
};
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 | func powerfulIntegers(x int, y int, bound int) (ans []int) {
s := map[int]struct{}{}
for a := 1; a <= bound; a *= x {
for b := 1; a+b <= bound; b *= y {
s[a+b] = struct{}{}
if y == 1 {
break
}
}
if x == 1 {
break
}
}
for x := range s {
ans = append(ans, x)
}
return ans
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 | function powerfulIntegers(x: number, y: number, bound: number): number[] {
const ans = new Set<number>();
for (let a = 1; a <= bound; a *= x) {
for (let b = 1; a + b <= bound; b *= y) {
ans.add(a + b);
if (y === 1) {
break;
}
}
if (x === 1) {
break;
}
}
return Array.from(ans);
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 | /**
* @param {number} x
* @param {number} y
* @param {number} bound
* @return {number[]}
*/
var powerfulIntegers = function (x, y, bound) {
const ans = new Set();
for (let a = 1; a <= bound; a *= x) {
for (let b = 1; a + b <= bound; b *= y) {
ans.add(a + b);
if (y === 1) {
break;
}
}
if (x === 1) {
break;
}
}
return [...ans];
};
|