-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPowerfulIntegers
39 lines (33 loc) · 1010 Bytes
/
PowerfulIntegers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/******************************/
Given two positive integers x and y, an integer is powerful if it is equal to x^i + y^j for some integers i >= 0 and j >= 0.
Return a list of all powerful integers that have value less than or equal to bound.
You may return the answer in any order. In your answer, each value should occur at most once.
Example 1:
Input: x = 2, y = 3, bound = 10
Output: [2,3,4,5,7,9,10]
Explanation:
2 = 2^0 + 3^0
3 = 2^1 + 3^0
4 = 2^0 + 3^1
5 = 2^1 + 3^1
7 = 2^2 + 3^1
9 = 2^3 + 3^0
10 = 2^0 + 3^2
/******************************/
class Solution {
public:
vector<int> powerfulIntegers(int x, int y, int bound)
{
set<int> res;
for(int i=0;i<21;i++)
for(int j=0;j<21;j++)
{
int temp=pow(x,i)+pow(y,j);
if(temp<=bound&&temp>0)//用temp>0排除溢出数据
{
res.insert(temp);
}
}
return vector<int>(res.begin(),res.end());
}
};