给定两个非负整数 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. NX二次开发-UFUN工程图表格注释设置单元格首选项UF_TABNOT_set_cell_prefs

    NX9+VS2012 #include <uf.h> #include <uf_tabnot.h> #include <NXOpen/Part.hxx> #incl ...

  2. 大数据-KNN算法

    KNN是通过测量不同特征值之间的距离进行分类.它的思路是:如果一个样本在特征空间中的k个最相似(即特征空间中最邻近)的样本中的大多数属于某一个类别,则该样本也属于这个类别,其中K通常是不大于20的整数 ...

  3. 文本数据增量导入到mysql

    实现思路:       实现Java读取TXT文件中的内容并存到内存,将内存中的数据和mysql 数据库里面某张表数据的字段做一个比较,如果比较内存中的数据在mysql 里存在则不做处理,如果不存在则 ...

  4. pop&dismiss

    //dismiss到根视图 - (void)dismissToRootViewController{ UIViewController *vc = self; while (vc.presenting ...

  5. innodb 表

    1.innodb的存储引擎表类型 如果在创建表时没有显示的定义主键,则innodb存储引擎会按如下方式选择或创建主键 a.首先表中是否有非空的唯一约束(Unique not null)如果有,则该列即 ...

  6. Face-Resources

    Face-Resources Following is a growing list of some of the materials I found on the web for research ...

  7. PHP算法之IP 地址无效化

    给你一个有效的 IPv4 地址 address,返回这个 IP 地址的无效化版本. 所谓无效化 IP 地址,其实就是用 "[.]" 代替了每个 ".". 示例 ...

  8. Python学习笔记(五)——异常处理

    Python 异常总结 异常名称 解释 AssertionError 断言语句(assert)失败:当assert关键字后边的条件为假时,程序将抛出该异常,一般用于在代码中置入检查点 OSError ...

  9. delphi 第3课

    (1)主程序:汇总或者记载 Delphi应用程序是以窗体为中心的 (1) 1 (2) 控制语句 if 条件 then 语句1: else 语句2: 2018-04-22   21:47:17

  10. Android开发 自定义View_白色圆型涟漪动画View

    代码: import android.animation.ValueAnimator; import android.content.Context; import android.graphics. ...