【LeetCode】Powerful Integers(强整数)
这道题是LeetCode里的第970道题。
题目描述:
给定两个正整数
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 <= 1001 <= y <= 1000 <= bound <= 10^6
如果用普通的数组保存结果的话,出现相同值的概率比较大,反正题目提示的也是哈希表,那我们就用 哈希集合(HashSet) 来保存结果呗。简单省事。
首先我们先要求出 x,y 的最大幂,确定 for 循环的次数,然后在一一带入值,把最终结果保存。
提交代码:
class Solution {
public List<Integer> powerfulIntegers(int x, int y, int bound) {
List<Integer>res=new ArrayList<>();
Set<Integer>set=new HashSet<>();
int m=0;
int n=0;
int num1=bound;
int num2=bound;
if(x==1)m=1;
else{
while(num1/x>0){
num1/=x;
m++;
}
}
if(y==1)n=1;
else{
while(num2/y>0){
num2/=y;
n++;
}
}
for(int i=0;i<=m;i++){
for(int j=0;j<=n;j++){
int intNum=(int)(Math.pow(x,i)+Math.pow(y,j));
if(intNum<=bound)set.add(intNum);
}
}
res.addAll(set);
return res;
}
}
提交结果:

个人总结:
没考虑到 x=1,y=1 时的特殊情况。暂时还不知道是否有更好的办法。因为数学这个东西很神奇。
【LeetCode】Powerful Integers(强整数)的更多相关文章
- LeetCode 970. Powerful Integers (强整数)
题目标签:HashMap 题目让我们找出所有独一的powerful integers 小于bound的情况下. 把 x^i 看作 a:把 y^j 看作b, 代入for loop,把所有的情况都遍历一遍 ...
- Leetcode970. Powerful Integers强整数
给定两个非负整数 x 和 y,如果某一整数等于 x^i + y^j,其中整数 i >= 0 且 j >= 0,那么我们认为该整数是一个强整数. 返回值小于或等于 bound 的所有强整数组 ...
- [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 ...
- 【LeetCode】970. Powerful Integers 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力搜索 日期 题目地址:https://leetc ...
- Leetcode 970. 强整数
970. 强整数 显示英文描述 我的提交返回竞赛 用户通过次数223 用户尝试次数258 通过次数231 提交次数801 题目难度Easy 给定两个正整数 x 和 y,如果某一整数等于 x^i ...
- 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 ...
- 【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 ...
- C#版 - Leetcode 13. 罗马数字转整数 - 题解
C#版 - Leetcode 13. 罗马数字转整数 - 题解 Leetcode 13. Roman to Integer 在线提交: https://leetcode.com/problems/ro ...
- LeetCode:罗马数字转整数【13】
LeetCode:罗马数字转整数[13] 题目描述 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 10 ...
随机推荐
- 关于nodejs模块安装后找不到包解决办法
主要原因是类似bower.gulp这些包后,没有添加到环境变量,但是有洁癖的我也不希望添加太多的软链接,所以在用phpstorm开始时有需要的情况下 定义临时的环境变量 http://stackove ...
- 1058 合唱队形 2004年NOIP全国联赛提高组
1058 合唱队形 2004年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Descripti ...
- ios UITableView顶部向下偏移
//设置向下偏移20[self.tableView setContentInset:UIEdgeInsetsMake(20,0,0,0)];
- COGS 615. 韩国明星
[问题描述] 在LazyCat同学的影响下,Roby同学开始听韩国的音乐,并且越来越喜欢H.o.T,尤其喜欢安七炫和Tony,可是,爱学习爱思考的Roby同学想,如果以后喜欢的韩星越来越多怎么办呢?R ...
- Asp.net Mvc 表单验证(气泡提示)
将ASP.NET MVC或ASP.NET Core MVC的表单验证改成气泡提示: //新建一个js文件(如:jquery.validate.Bubble.js),在所有要验证的页面引用 (funct ...
- “流”的思维—Workflowy
3.“流”的思维—Workflowy是我最喜欢的”流“的工具(WorkFlowy - Organize your brain.)我觉得,让发散性的思维更具实施性,必须分步操作,必须有先后,必须单线程. ...
- WPF中窗体调用窗体
在WPF中有时候我们需要在一个窗体中去调用另外的一个窗体,下面给出调用方法. 下面实现在MainWindow中通过点击一个按钮调用另外的一个窗口. 首先创建你要调用的另外一个窗口:点击最上面的项目 ...
- webpack 使用总结
参考:http://www.ferecord.com/webpack-summary.html#base64 写的比较详细了
- 解析IPV4报文 和IPV6 报文的 checksum
解析IPV4报文和IPV6报文的checksum的算法: 校验和(checksum)算法,简单的说就是16位累加的反码运算: 计算函数如下: 我们在计算时是主机字节序,计算的结果封装成IP包时是网络字 ...
- MySQL 查询优化之 Index Condition Pushdown
MySQL 查询优化之 Index Condition Pushdown Index Condition Pushdown限制条件 Index Condition Pushdown工作原理 ICP的开 ...