给定两个非负整数 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. Model Maker上手指南

    Model Maker上手指南 目录 1.MM可爱的脸 2.MM中的工程Project 3.新建类图 4.添加类成员 5.实现类的方法 6.生成Delphi代码 7.逆向到模型 8.完全的逆向工程 作 ...

  2. uoj213 【UNR #1】争夺圣杯

    题目 设\(f_i\)表示所有长度为\(i\)的区间的最大值的和,求\(\bigoplus \sum_{i=1}^nf_i\) 不难发现随机数据非常好做 由于一个随机序列的前缀最大值期望只会变化\(\ ...

  3. 2018今日头条湖北省赛【H】

    [题目链接]https://www.nowcoder.com/acm/contest/104/G 现场赛的H题,emmm...C++选手表示很伤心.高精度压四位板子WA四发. 题意很简单就是给你n个数 ...

  4. jmeter在windows环境下系统参数设置

    在windows环境下搭建jmeter的压测实验环境,需要对操作系统默认的一些个参数进行设置,以提高并发能力.特别是作为压力机的时候. Socket 编程时,单机最多可以建立多少个 TCP 连接,受到 ...

  5. Activiti学习笔记目录

    1.Activiti学习笔记1 — 下载与开发环境的配置: 2.Activiti学习笔记2 — HelloWorld: 3.Activiti学习笔记3 — 流程定义: 4.Activiti学习笔记4 ...

  6. iOS开发系列-Shell脚本编译SDK

    Library静态库Shell脚本 #!/bin/bash #要build的target名 target_Name="IFlyMSC" #编译模式 Release.Debug bu ...

  7. iOS开发系列-应用程序之间跳转

    概述 常见的涉及到应用程序之间的跳转场景,比如社交分享.支付宝.微信支付.链接跳转到应用. 在iOS中应用跳转的本质:打开一个应用只需要拿到对应应用的URL即可. 统一资源定位符 URL(统一资源定位 ...

  8. iOS开发系列-LLVM、Clang

    LLVM LLVM计划启动于2000年,最初由University of Illinois at Urbana-Champaign的Chris Lattner主持开展. 我们可以认为LLVM是一个完整 ...

  9. openwrt_ipsec_function.sh 分析

    #!/bin/sh # # Copyright (C) Vitaly Protsko <villy@sft.ru> errno= # get_fieldval gate src " ...

  10. 阿里云应用上边缘云解决方案助力互联网All in Cloud

    九月末的杭州因为一场云栖大会变得格外火热. 9月25日,吸引全球目光的2019杭州云栖大会如期开幕.20000平米的展区集结数百家企业,为数万名开发者带来了一场前沿科技的饕餮盛宴. 如同往年一样,位于 ...