970. Powerful Integers
Given three integers x, y, and bound, return a list of all the powerful integers that have a value less than or equal to bound.
An integer is powerful if it can be represented as xi + yj for some integers i >= 0 and j >= 0.
You may return the answer in any order. In your answer, each value should occur at most once.
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
| class Solution { public: vector<int> powerfulIntegers(int x, int y, int bound) { if(bound < 2) return {}; if(x < y) return powerfulIntegers(y, x, bound); if(x == 1 and y == 1) return {2}; if(y == 1) { vector<int> res{2}; int p = 1; while(y + pow(x,p) <= bound) { res.push_back(y + pow(x, p)); p++; } return res; } unordered_set<int> us; int p = 0; while(pow(x,p) + 1 <= bound) { for(int i = 0, X = pow(x, p); X + pow(y,i) <= bound; i++) { us.insert(X + pow(y,i)); } p++; } return vector<int>(begin(us), end(us)); } };
|