【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 ...
随机推荐
- 我的NopCommerce之旅(5): 缓存
一.基础介绍 1.什么是cache Web缓存是指一个Web资源(如html页面,图片,js,数据等)存在于Web服务器和客户端(浏览器)之间的副本. 2.为什么要用cache 即 ...
- 借助sass的Maps功能使得响应式代码更有条理
原文来自这里 本文综合了原文(by Jonathan Suh)以及笔者自己的理解. Introduction 众所周知,写代码与写维护性高的代码是两回事.而涉及到响应式,代码又特别容易变的杂乱.借助s ...
- AJPFX分享JAVA修饰符详解
1.类的修饰符分为:可访问控制符和非访问控制符两种. 可访问控制符是:公共类修饰符 public 非访问控制符有:抽象类修饰符 abstract :最终类修饰符 final 1 )公共类修饰符 pub ...
- 总结React关于require的问题
我需要实现的是当登录页面传过来的sex值为1则性别一栏的图片修改为boy.png如果为0性别图片则显示为girl.png‘ 最开始是这么写的为了让他成为变量 所以不行ok我们回到React的生命周期函 ...
- Django模板导入及母版继承和自定义返回Html片段
1.ROOT_URLCONF = '总路由所在路径(比如untitled.urls)'<===默认情况是这样 根路由的路径是可以修改的:ROOT_URLCONF = app01.urls< ...
- ubuntu 16.04 国内源安装docker
1. 通过curl命令安装 检查是否安装curl root@ros-OptiPlex-3050:~# which curlroot@ros-OptiPlex-3050:~# 更新安装 root@ros ...
- 如何解决源码安装软件中make时一直重复打印configure信息
在通过源码安装软件时,会出现执行./configure后再make时总是重复打印configure的信息,无法进入下一阶段的安装. 主要原因是系统当前的时间与实际时间不一致,特别是在虚拟机上经常会出现 ...
- Ubuntu14.04 32位安装Youcompleteme
前一段时间在ubuntu16.04 64位上安装了vim插件Youcompleteme,花了两三天才弄好.今天在ubuntu14.04 32位上安装同样的插件,才知道之前所做的安装原来是多么的简单.今 ...
- WPF中单选框RadioButton
单选框RadioButton的基本使用: <StackPanel Margin="10"> <Label FontWeight="Bold"& ...
- ArcMap所有Command GUID
The information in this topic is useful if you're trying to programmatically find a built-in command ...