给定两个非负整数 x 和 y,如果某一整数等于 x^i + y^j,其中整数 i >= 0 且 j >= 0,那么我们认为该整数是一个强整数。

返回值小于或等于 bound 的所有强整数组成的列表。

你可以按任何顺序返回答案。在你的回答中,每个值最多出现一次。

示例 1:

输入:x = 2, y = 3, bound = 10 输出:[2,3,4,5,7,9,10] 解释: 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

示例 2:

输入:x = 3, y = 5, bound = 15 输出:[2,4,6,8,10,14]

提示:

  • 1 <= x <= 100
  • 1 <= y <= 100
  • 0 <= bound <= 10^6

暴力法

如果枚举x的指数,那么每次选择指数后都要去得到x的幂运算结果。每次都进行幂运算,那么就重复了很多操作。所以先把幂运算的结果保存下来,去枚举幂运算的结果

class Solution {
public:
vector<int> powerfulIntegers(int x, int y, int bound)
{
vector<int> vx;
vector<int> vy;
vector<int> ans;
map<int, bool> check;
int tempx = 1;
int tempy = 1;
if(x == 1)
{
vx.push_back(1);
}
else
{
while(tempx <= bound)
{
vx.push_back(tempx);
tempx *= x;
}
}
if(y == 1)
{
vy.push_back(1);
}
else
{
while(tempy <= bound)
{
vy.push_back(tempy);
tempy *= y;
}
}
for(int i = 0; i < vx.size(); i++)
{
for(int j = 0; j < vy.size(); j++)
{
if(vx[i] + vy[j] <= bound && !check[vx[i] + vy[j]])
{
check[vx[i] + vy[j]] = 1;
ans.push_back(vx[i] + vy[j]);
}
}
}
return ans;
}
};

通过这道题突然想到了另外一道题,用的很简便神奇的方法。

题目描述

把只包含质因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含质因子7。 习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。

class Solution {
public:
int GetUglyNumber_Solution(int index) {
if(index <= 0)
return index;
vector<int> res(index);
res[0] = 1;
int t2 = 0, t3 = 0, t5 = 0;
for(int i = 1; i < index; i++)
{
res[i] = min(res[t2] * 2, min(res[t5] * 5, res[t3] * 3));
//不用else if的原因是为了去重
if(res[i] == res[t2] * 2)
t2++;
if(res[i] == res[t3] * 3)
t3++;
if(res[i] == res[t5] * 5)
t5++;
}
return res[index - 1];
}
};

Leetcode970. Powerful Integers强整数的更多相关文章

  1. 【LeetCode】Powerful Integers(强整数)

    这道题是LeetCode里的第970道题. 题目描述: 给定两个正整数 x 和 y,如果某一整数等于 x^i + y^j,其中整数 i >= 0 且 j >= 0,那么我们认为该整数是一个 ...

  2. LeetCode 970. Powerful Integers (强整数)

    题目标签:HashMap 题目让我们找出所有独一的powerful integers 小于bound的情况下. 把 x^i 看作 a:把 y^j 看作b, 代入for loop,把所有的情况都遍历一遍 ...

  3. [Swift]LeetCode970.强整数 | Powerful Integers

    Given two non-negative integers x and y, an integer is powerful if it is equal to x^i + y^j for some ...

  4. LeetCode970. 强整数

    问题:970. 强整数 用户通过次数0 用户尝试次数0 通过次数0 提交次数0 题目难度Easy 给定两个非负整数 x 和 y,如果某一整数等于 x^i + y^j,其中整数 i >= 0 且  ...

  5. Leetcode 970. 强整数

    970. 强整数  显示英文描述 我的提交返回竞赛   用户通过次数223 用户尝试次数258 通过次数231 提交次数801 题目难度Easy 给定两个正整数 x 和 y,如果某一整数等于 x^i ...

  6. 118th LeetCode Weekly Contest Powerful Integers

    Given two non-negative integers x and y, an integer is powerful if it is equal to x^i + y^j for some ...

  7. 【Leetcode_easy】970. Powerful Integers

    problem 970. Powerful Integers solution: class Solution { public: vector<int> powerfulIntegers ...

  8. LC 970. Powerful Integers

    Given two non-negative integers x and y, an integer is powerful if it is equal to x^i + y^j for some ...

  9. 【leetcode】970. Powerful Integers

    题目如下: Given two non-negative integers x and y, an integer is powerful if it is equal to x^i + y^j fo ...

随机推荐

  1. Dll注入技术之远程线程注入

    DLL注入技术之远线程注入 DLL注入技术指的是将一个DLL文件强行加载到EXE文件中,并成为EXE文件中的一部分,这样做的目的在于方便我们通过这个DLL读写EXE文件内存数据,(例如 HOOK EX ...

  2. ionic:创建 APP

    ylbtech-ionic:创建 APP 1.返回顶部 1. ionic 创建 APP 前面的章节中我们已经学会了 ionic 框架如何导入到项目中. 接下来我们将为大家介绍如何创建一个 ionic ...

  3. hadoop2.x需要知道的默认yarn配置

    在Hadoop 2.2.0中,YARN框架有很多默认的参数值,如果你是在机器资源比较不足的情况下,需要修改这些默认值,来满足一些任务需要.NodeManager和ResourceManager都是在y ...

  4. Python实现全局变量的两个解决方法

    Python实现全局变量的两个解决方法 本文针对Python的全局变量实现方法简述如下: 先来看下面一段测试程序:     count = 0 def Fuc(count):   print coun ...

  5. 剑指offer——17数值的整数次方

    题目描述 给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方.   保证base和exponent不同时为0   一般解法: 直接相乘: cl ...

  6. java.lang.NoSuchMethodError: com.google.common.hash.HashFunction.hashInt(I)Lcom/google/common/hash/HashCode; 解决办法

    今天在java 上运行spark查询的时候出现一个问题: java.lang.NoSuchMethodError: com.google.common.hash.HashFunction.hashIn ...

  7. Scala 可变长参数

    Scala 允许你指明函数的最后一个参数可以是重复的.这可以允许客户向函数传入可变长度参数列表.想要标注一个重复参数,在参数的类型之后放一个星号. 例如:定义一个可变参数的函数param def pa ...

  8. 17-Ubuntu-文件和目录命令-切换目录-相对路径和绝对路径

    1.相对路径: 在输入路径时,最前面不是/或者~,表示相对当前目录所在的目录位置. 例:当前桌面目录下,通过相对路径切换到桌面目录下的Entertainment目录 2.绝对路径: 在输入路径时,最前 ...

  9. 科普帖:深度学习中GPU和显存分析

    知乎的一篇文章: https://zhuanlan.zhihu.com/p/31558973 关于如何使用nvidia-smi查看显存与GPU使用情况,参考如下链接: https://blog.csd ...

  10. 与960 Grid System相关的那些问题

    为什么是960px? 一直以来,网页设计师都希望寻找一个理想的页面宽度值,既能适应大部分屏幕,又尽可能的在一行显示更多的信息. 我们首先会考虑的是全屏自适应,但这并非一个好的解决方案.一方面,需要做一 ...