LeetCode970. 强整数
问题: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. 强整数的更多相关文章
- [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 ...
- Leetcode970. Powerful Integers强整数
给定两个非负整数 x 和 y,如果某一整数等于 x^i + y^j,其中整数 i >= 0 且 j >= 0,那么我们认为该整数是一个强整数. 返回值小于或等于 bound 的所有强整数组 ...
- Leetcode 970. 强整数
970. 强整数 显示英文描述 我的提交返回竞赛 用户通过次数223 用户尝试次数258 通过次数231 提交次数801 题目难度Easy 给定两个正整数 x 和 y,如果某一整数等于 x^i ...
- 【LeetCode】Powerful Integers(强整数)
这道题是LeetCode里的第970道题. 题目描述: 给定两个正整数 x 和 y,如果某一整数等于 x^i + y^j,其中整数 i >= 0 且 j >= 0,那么我们认为该整数是一个 ...
- LeetCode 970. Powerful Integers (强整数)
题目标签:HashMap 题目让我们找出所有独一的powerful integers 小于bound的情况下. 把 x^i 看作 a:把 y^j 看作b, 代入for loop,把所有的情况都遍历一遍 ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- PJzhang:python基础进阶的10个疗程-three
猫宁!!! 参考:北京理工大学-python程序语言设计 第3节:基本数据类型 默写代码的重要性. 保留字一定要全部都会默写,不能有错. pow(x,y),计算x的y次方,整数没有大小限制 整数进制 ...
- 基于Visual C++2013拆解世界五百强面试题--题2-输出和为一个给定整数的所有组合
请用C语言实现 输出和为一个给定整数的所有组合 启动2012 /* 请用C语言实现 输出和为一个给定整数的所有组合 */ #include <stdio.h> //包含头文件stdio.h ...
- 李洪强漫谈iOS开发[C语言-051]-判断整数位数
随机推荐
- spring的IOC和AOP详细讲解
1.解释spring的ioc? 几种注入依赖的方式?spring的优点? IOC你就认为他是一个生产和管理bean的容器就行了,原来需要在调用类中new的东西,现在都是有这个IOC容器进行产生,同时, ...
- Vue.js基础语法(二)组件
vue学习的一系列,全部来自于表哥---表严肃,是我遇到过的讲课最通透,英文发音最好听的老师,想一起听课就去这里吧 https://biaoyansu.com/i/hzhj1206 把一段经常要用的东 ...
- Swift UI开发初探 (转)
原文地址:http://www.tairan.com/archives/6600 关于Swift语法,可以参考<Apple Swift编程语言入门教程> 效果如下: 开发环境 Xcode6 ...
- jquery_lazyload插件
延迟加载图片的 jQuery 插件 http://www.neoease.com/lazy-load-jquery-plugin-delay-load-image/
- TeeChart .NET for iOS图表开发入门教程
去年,TeeChart 为iOS图表开发专门发布了TeeChart NET for iOS(包含在TeeChart Mobile中),相信很多人都对其感兴趣.慧都为大家制作了TeeChart NET ...
- http头部如何对缓存的控制
文章自于我的个人博客 使用缓存的目的就是在于减少计算,IO,网络等时间,可以快速的返回,特别是流量比较大的时候,可以节约很多服务器带宽和压力. 一个请求从缓存的方面来说,有三个过程. 本地检查缓存是否 ...
- Linux命令之查看服务进程(ps aux、ps -aux、ps -ef)的运用
执行ps命令即可列出的是当前服务器进程的快照(时间点),如果想要实时动态的显示进程信息,就可以使用top命令. linux上进程有5种状态: 1. 运行(正在运行或在运行队列中等待) 2. 中断( ...
- UITabBarController动态添加TabBarItem
NSArray *titles = @[L(@"首页"), L(@"新闻"), L(@"消息"), L(@"我的")]; ...
- 基于FPGA的VGA显示设计(一)
前言 FPGA主要运用于芯片验证.通信.图像处理.显示VGA接口的显示器是最基本的要求了. 原理 首先需要了解 : (1)VGA接口协议:VGA端子_维基百科 .VGA视频传输标准_百度 引脚1 RE ...
- 【2017-07-04】Qt信号与槽深入理解之一:信号与槽的连接方式
今天是个好日子,嗯. 信号槽机制是Qt的特色功能之一,类似于windows中的消息机制,在不同的类对象间传递消息时我们经常使用信号槽机制,然而很多时候都没有去关注connect()函数到底有几种重载的 ...