LOADING

加载过慢请开启缓存 浏览器默认开启

leetcode 970

题目:

给定三个整数x 、y和bound,返回值小于或等于bound的所有强整数 组成的列表 。如果某一整数可以表示为 $x^i + y^j$ ,其中整数 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 <= $10^6$

思路

#include <bits/stdc++.h>
using namespace std;
//使用set暴力循环
class Solution
{
public:
    vector<int> powerfulIntegers(int x, int y, int bound)
    {
        vector<int> ans;
        unordered_set<int> res;
        for (int i = 1; i <= bound; i *= x)
        {
            for (int j = 1; j + i <= bound; j *= y)
            {
                res.insert(i + j);
                if (y == 1)
                    break;
            }
            if (x == 1)
                break;
        }
        for (int x : res)
            ans.push_back(x);
        sort(ans.begin(), ans.end());
        return ans;
    }
}
本文作者:GWB
当前时间:2023-11-09 11:11:11
版权声明:本文由gwb原创,本博客所有文章除特别声明外,均采用 CC BY-NC-ND 4.0 国际许可协议。
转载请注明出处!