Skip to content

Commit c042108

Browse files
Create 970. 强整数.md
1 parent bf351b2 commit c042108

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

Enumerate/970. 强整数.md

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#### 970. 强整数
2+
3+
难度:中等
4+
5+
---
6+
7+
给定三个整数 `x` 、 `y` 和 `bound` ,返回 _值小于或等于 `bound` 的所有  **强整数**  组成的列表_ 。
8+
9+
如果某一整数可以表示为 `x^i + y^j` ,其中整数 `i >= 0` 且 `j >= 0`,那么我们认为该整数是一个  **强整数**  。
10+
11+
你可以按 **任何顺序** 返回答案。在你的回答中,每个值 **最多** 出现一次。
12+
13+
**示例 1:**
14+
15+
```
16+
输入:x = 2, y = 3, bound = 10
17+
输出:[2,3,4,5,7,9,10]
18+
解释:
19+
2 = 20 + 30
20+
3 = 21 + 30
21+
4 = 20 + 31
22+
5 = 21 + 31
23+
7 = 22 + 31
24+
9 = 23 + 30
25+
10 = 20 + 32
26+
```
27+
28+
**示例 2:**
29+
30+
```
31+
输入:x = 3, y = 5, bound = 15
32+
输出:[2,4,6,8,10,14]
33+
```
34+
35+
**提示:**
36+
37+
* `1 <= x, y <= 100`
38+
* `0 <= bound <= 10^6`
39+
40+
---
41+
42+
枚举:
43+
44+
控制循环的范围即可,并且需要用 `HashSet` 去除重复答案。
45+
46+
```Java
47+
class Solution {
48+
public List<Integer> powerfulIntegers(int x, int y, int bound) {
49+
Set<Integer> set = new HashSet<Integer>();
50+
for(int i = 1; i <= bound; i *= x){
51+
for(int j = 1; i + j <= bound; j *= y){
52+
set.add(i + j);
53+
if(y == 1) break;
54+
}
55+
if(x == 1) break;
56+
57+
}
58+
return new ArrayList<Integer>(set);
59+
}
60+
}
61+
```

0 commit comments

Comments
 (0)