问题:970. 强整数

  • 用户通过次数0
  • 用户尝试次数0
  • 通过次数0
  • 提交次数0
  • 题目难度Easy

给定两个非负整数 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

链接:https://leetcode-cn.com/contest/weekly-contest-118/problems/powerful-integers/

分析:

数据范围并不大,只需要列出所有的Xs=X^i<=bound,Ys=Y^i<=bound,然后找到满足Xs+Ys<=bound即可。

需要注意的有

1.如果x/等于1,对应的列表里面只会有1

2,有可能X^i11+Y^i12==X^i21+Y^i22,且i11!=i21,i12!=i22,比如2^4+4^0=2^0+4^2,所以需要对和去重。

AC Code:

 class Solution {
public:
vector<int> powerfulIntegers(int x, int y, int bound) {
vector<int> ret; vector<int> xs;
vector<int> ys;
int tmp = ;
while (true)
{
if (x == )
{
xs.emplace_back(x);
break;
}
int local = pow(x, tmp);
if (local > bound)
{
break;
}
else
{
xs.emplace_back(local);
tmp++;
}
} tmp = ;
while (true)
{
if (y == )
{
ys.emplace_back(y);
break;
}
int local = pow(y, tmp);
if (local > bound)
{
break;
}
else
{
ys.emplace_back(local);
tmp++;
}
}
set<int> nums;
for (int i = ; i < xs.size(); i++)
{
for (int j = ; j < ys.size(); j++)
{
int local = xs[i] + ys[j];
if (local > bound)
{
break;
}
else
{
nums.insert(local);
}
}
}
for (set<int>::iterator it = nums.begin(); it != nums.end(); it++)
{
ret.emplace_back(*it);
}
return ret;
} };

其他:

国内第一code:

class Solution(object):
def powerfulIntegers(self, x, y, bound):
"""
:type x: int
:type y: int
:type bound: int
:rtype: List[int]
"""
if x > y:
x, y = y, x
ans = set()
for i in range():
for j in range():
if x ** i + y ** j <= bound:
ans.add(x ** i + y ** j)
return list(ans)

1<=x,y<=100,0<=bound<=10^6,由于x,y取整数,所以除1外最小的2^20=1048576>10^6,20*20两层循环即可,甚至可以在外层判断如果过大直接结束,内层也是如果大于bound可以直接结束。

如下:

class Solution {
public:
vector<int> powerfulIntegers(int x, int y, int bound) {
vector<int> ret;
if (x < y)
{
int tmp = x;
x = y;
y = tmp;
}
for(int i=;i<;i++)
{
int tmp = pow(x, i);
if (tmp >= bound)
{
break;
}
for (int j = ; j < ; j++)
{
int tmpans =tmp+ pow(y, j);
if (tmpans > bound)
{
break;
}
else
{
if (count(ret.begin(), ret.end(), tmpans) == )
{
ret.emplace_back(tmpans);
}
}
}
}
return ret;
}
};

4ms战胜98.13% cpp code。

用时最短code:

class Solution {
public:
vector<int> powerfulIntegers(int x, int y, int bound) {
set<int> S;
int i, tx, ty;
for (tx = ; tx <= bound; tx *= x) {
for (ty = ; ty <= bound; ty *= y) {
if (tx + ty > bound)
break;
S.insert(tx + ty);
if (y == )
break;
}
if (x == )
break;
}
auto it = S.begin();
vector<int> ans;
while (it != S.end()) {
ans.push_back(*it);
it++;
}
return ans; }
};

大概逻辑差不多,用set去重,计算幂的和,对比bound,对于1只用一次跳过循环

LeetCode970. 强整数的更多相关文章

  1. [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 ...

  2. Leetcode970. Powerful Integers强整数

    给定两个非负整数 x 和 y,如果某一整数等于 x^i + y^j,其中整数 i >= 0 且 j >= 0,那么我们认为该整数是一个强整数. 返回值小于或等于 bound 的所有强整数组 ...

  3. Leetcode 970. 强整数

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

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

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

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

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

  6. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  7. PJzhang:python基础进阶的10个疗程-three

    猫宁!!! 参考:北京理工大学-python程序语言设计 第3节:基本数据类型 默写代码的重要性. 保留字一定要全部都会默写,不能有错. pow(x,y),计算x的y次方,整数没有大小限制 整数进制 ...

  8. 基于Visual C++2013拆解世界五百强面试题--题2-输出和为一个给定整数的所有组合

    请用C语言实现 输出和为一个给定整数的所有组合 启动2012 /* 请用C语言实现 输出和为一个给定整数的所有组合 */ #include <stdio.h> //包含头文件stdio.h ...

  9. 李洪强漫谈iOS开发[C语言-051]-判断整数位数

随机推荐

  1. VMware 虚拟机(linux)增加根目录磁盘空间

    VMware 虚拟机(linux)增加根目录磁盘空间     今天查看学校的监控报修系统,不能访问了!!!系统运行很慢,用top命令查看发现内存使用率90%,用"df -h ”查看“/”目录 ...

  2. jenkins构建&发布git托管的VS工程

    顺便做个笔记,以防以后再踩坑:笔者用的是jenkins-1.620和git-2.7.2-64-bit.exe,操作系统是win10(本机) jenkins安装完成之后 如果想要配合git完成拉取代码. ...

  3. Spring课程 Spring入门篇 3-5 Spring bean装配(上)之Resource

    课程链接: 1 resource简析 2 resource代码演练 1 resource简析 urlsource:url对应的资源 classpath:获取类路径下的资源文件 filesystemre ...

  4. intellijidea课程 intellijidea神器使用技巧 3-1 列操作

    Ctrl shift 右箭头 ==> 选中右边单词 ctrl shift alt L ==> 选中所有相同的字符 Ctrl alt L     ==> 格式化

  5. php数组的定义

    数组的主要作用就是存储数据,修改数据. 数组:就是把多个数据放在一起管理. 数组可以存入多个不同类型的数据,是一个复合数据类型.   在PHP中,有两种方式定义数组 //$array = array( ...

  6. sharepoint国内网站一览表(转发)

    中国石油化工集团公司http://www.sinopecgroup.com/Pages/index.aspx () 中国南方航空http://group.csair.com/_layouts/grou ...

  7. 视频SharePoint 2010 大局观 By 陈希章[zt]

    SharePoint 2010 大局观 By 陈希章 http://hi.baidu.com/jinzesudawei/item/9d0adace8cbcaf2c47d5c0fc

  8. python-rrdtool

    https://nagios-plugins.org/doc/guidelines.html nagios检测信息 host GPING OK – rtt min/avg/max/mdev = 0.8 ...

  9. vos设置可呼出手机或固话

    问题: 默认公司只让呼出手机号码,但有的客户要求能打固话,怎么办? 落地网关——补充设置——落地前缀——落地被叫改写规则 在改写规则里添加固话号段即可 具体案例: 5201——1表示让520号段只能拨 ...

  10. jquery中对于ul>li列表分页。学习记录

    这个是很简单的一种分页,只能对列表进行分页.为了开发有可能需要用到记录下来 Html代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 T ...