【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 ...
随机推荐
- Java语言中自动生成随机数
参考原文:http://zhidao.baidu.com/link?url=nB3BT69wmUAiSPfKGgK5Q7HOFFP9AIE04AthreRd9yYcwKhUQsQRWlMdMhW1Qm ...
- spark中产生shuffle的算子
Spark中产生shuffle的算子 作用 算子名 能否替换,由谁替换 去重 distinct() 不能 聚合 reduceByKey() groupByKey groupBy() groupByKe ...
- 【cpp】new delete
double *M = new double[2*num]; double *T = new double[2 * num]; double *activeM = new double[2 * num ...
- win7 ghost 纯净版最新系统下载
这个系统是WIN7系统GHOST版装机旗舰版 SP1,更新了系统补丁到2016-02(可通过微软漏洞扫描和卫士漏洞扫描),升级Internet Explorer为IE9,增加数款驱动的支持,支持最新的 ...
- iOS打包上传app store各种问题解决总结
问题1 this action could not be completed. try again 问题2 there was an error sending data to the iTunes ...
- Objective-C中的命名前缀说明
http://www.cnblogs.com/dhui69/p/6410134.html __kindof __kindof 这修饰符还是很实用的,解决了一个长期以来的小痛点,拿原来的 UITable ...
- 简单的 创建AJax的方法
// 简单的ajax对象 var myAjax = { // XMLHttpRequest IE7+, Firefox, Chrome, Opera, Safari : ActiveXObject I ...
- JavaScript异步仿同步(控制流)的实现
在前端开发中尤其是在nodejs开发中经常会遇到这样的场景(以ajax为例):有3个(或者更多个)Ajax请求,并且第2个请求依赖于第1个,第3个请求依赖于第2个,那我们可能就会在发第一个Ajax后回 ...
- 基于KMeans的指数择时策略
[导语]:聚类分析是指将物理或者抽象对象的结合分组为由类似对象组成的多个类的分析过程.简单来讲,聚类就是通过一些特征去自动识别一个大群体中的多个子群体,这些子群体中的对象彼此之间相似度高,而子群体之间 ...
- Docker和K8S
干货满满!10分钟看懂Docker和K8S [摘自:https://my.oschina.net/jamesview/blog/2994112] 本文来源微信号:鲜枣课堂 2010年,几个搞IT的 ...