问题: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. 同源策略引发对跨域jsonp跨域的理解

    一,同源策略其实网络的安全基石,既:http://www.baidu.com:80协议(http或者HTTPS或者ws或者wss).域名(www.baidu.com).端口(默认80,可以不写 htt ...

  2. Sql 本周当天本期日期转换

    --查询当天: --查询24小时内的: --info为表名,datetime为数据库中的字段值 --查询当天: --查询24小时内的: select * from table where DateDi ...

  3. linux创建用户与删除用户及问题解决(ubuntu)

    创建的用户不正确,一直在删除创建,发现了挺多问题也学到了一些东西如下是我的总结. (root用户设置: 由于ubtun系统默认是没有激活root用户的,需要我们手工进行操作,在命令行界面下,或者在终端 ...

  4. js数组Array方法

    1. indexOf indexOf()方法返回在该数组中第一个找到的元素位置,如果它不存在则返回-1. var fruits = ["Banana", "Orange& ...

  5. vue安装--使用node

    总结: # 全局安装 vue-cli $ npm install --global vue-cli # 创建一个基于 webpack 模板的新项目 $ vue init webpack my-proj ...

  6. (WPF) DataGrid之绑定

    通过ObservableCollection 绑定到 DataGrid. 1. 前台Xaml. <DataGrid x:Name="dgMeasurements" Horiz ...

  7. PhoneGap&jQuery Mobile应用开发环境配置(For Android)

    关于移动应用为什么用PhoneGap和jQuery Mobile本文不再赘述,有兴趣的童鞋可以自行问“度娘”,有很多这方面的文章.本文主要介绍PhoneGap&jQuery Mobile移动应 ...

  8. 阻塞IO, 非阻塞IO, 同步IO,异步IO

    阻塞IO, 非阻塞IO, 同步IO,异步IO 介绍 先说明几个概念 用户空间与内核空间 为了保证用户进程不能直接操作内核(kernel),保证内核的安全,操心系统将虚拟空间(内存)划分为两部分,一部分 ...

  9. oracle的数值数据类型和兼容细分类型

    Oracle存储数值类型的数据不区分int .double .float 等类型,统一使用number(p,s)来存储. 基本类型为 NUMBER(P,S) P范围1到38 S 范围 -84 到 12 ...

  10. Python3爬虫04(其他例子,如处理获取网页的内容)

    #!/usr/bin/env python# -*- coding:utf-8 -*- import osimport reimport requestsfrom bs4 import Navigab ...