LeetCode.970-强大的整数(Powerful Integers)
这是悦乐书的第367次更新,第395篇原创
01 看题和准备
今天介绍的是LeetCode算法题中Easy级别的第229题(顺位题号是970)。给定两个正整数x和y,如果对于某些整数i >= 0且j >= 0等于x^i + y^j,则整数是强大的。
返回值小于或等于bound的所有强大整数的列表。
你可以按任何顺序返回答案。在你的答案中,每个值最多应出现一次。例如:
输入: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
输入:x = 3,y = 5,bound = 15
输出:[2,4,6,8,10,14]
注意:
1 <= x <= 100
1 <= y <= 100
0 <= bound <= 10^6
02 第一种解法
直接翻译题目即可,没有什么特殊的技巧,但是需要注意一点,因为判断条件时x或者y的几次方小于bound,如果x或者y为1的时候,1的任何次方都会是1,会一直小于bound,会造成死循环。
public List<Integer> powerfulIntegers(int x, int y, int bound) {
Set<Integer> set = new HashSet<Integer>();
for (int i=0; Math.pow(x, i) < bound; i++) {
for (int j=0; Math.pow(y, j) < bound; j++) {
int sum = (int)Math.pow(x, i)+(int)Math.pow(y, j);
if (sum <= bound) {
set.add((int)sum);
}
// y等于1时,容易造成死循环,要结束掉
if (y == 1) {
break;
}
}
// x等于1时,容易造成死循环,要结束掉
if (x == 1) {
break;
}
}
return new ArrayList<>(set);
}
03 第二种解法
针对上面第一种解法,我们也可以不借助Math类的pow方法,用累计相乘替代,思路都是一样的。
public List<Integer> powerfulIntegers2(int x, int y, int bound) {
Set<Integer> set = new HashSet<Integer>();
for (int i=1; i<bound; i *= x) {
for (int j=1; j<bound; j *= y) {
if (i+j <= bound) {
set.add(i+j);
}
if (y == 1) {
break;
}
}
if (x == 1) {
break;
}
}
return new ArrayList<>(set);
}
04 小结
算法专题目前已连续日更超过七个月,算法题文章235+篇,公众号对话框回复【数据结构与算法】、【算法】、【数据结构】中的任一关键词,获取系列文章合集。
以上就是全部内容,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、转发就是对我最大的回报和支持!
LeetCode.970-强大的整数(Powerful Integers)的更多相关文章
- [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 (强整数)
题目标签:HashMap 题目让我们找出所有独一的powerful integers 小于bound的情况下. 把 x^i 看作 a:把 y^j 看作b, 代入for loop,把所有的情况都遍历一遍 ...
- 【LeetCode】970. Powerful Integers 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力搜索 日期 题目地址:https://leetc ...
- 【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 ...
- 【Leetcode_easy】970. Powerful Integers
problem 970. Powerful Integers solution: class Solution { public: vector<int> powerfulIntegers ...
- 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 ...
- LC 970. 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 ...
- 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 ...
随机推荐
- UVA - 11107 Life Forms (广义后缀自动机+后缀树/后缀数组+尺取)
题意:给你n个字符串,求出在超过一半的字符串中出现的所有子串中最长的子串,按字典序输出. 这道题算是我的一个黑历史了吧,以前我的做法是对这n个字符串建广义后缀自动机,然后在自动机上dfs,交上去AC了 ...
- 我说CMMI之一:CMMI是什么--转载
我说CMMI之一:CMMI是什么 有些朋友没有接触过CMMI,正在学习CMMI,CMMI本身的描述比较抽象,所以,读起来有些费劲.有些朋友实施过CMMI,但是可能存在对CMMI的一些误解,因此我想说说 ...
- DevExpress ASP.NET Bootstrap v19.1版本亮点:Editors控件
行业领先的.NET界面控件DevExpress 正式发布了v19.1版本,本文将以系列文章的方式为大家介绍DevExpress ASP.NET Bootstrap Controls中Editors.G ...
- 红帽Linux故障定位技术详解与实例(4)
红帽Linux故障定位技术详解与实例(4) 在线故障定位就是在故障发生时, 故障所处的操作系统环境仍然可以访问,故障处理人员可通过console, ssh等方式登录到操作系统上,在shell上执行 ...
- python基础语法-Ⅲ
Python注释 python中单行注释采用 # 开头. 实例 输出结果: 注释可以在语句或表达式行末: python 中多行注释使用三个单引号(''')或三个双引号(""&quo ...
- Nowcoder Removal ( 字符串上的线性 DP )
题目链接 题意 : 给出长度为 n 的字符串.问你准确删除 m 个元素之后.能产生多少种不同的子串 分析 ( 参考博客 ): 可以考虑线性 DP 解决这个问题 试着如下定义动态规划数组 dp[i][ ...
- Linux命令-磁盘管理(一)
Linux命令-磁盘管理(一) Linux cd命令 Linux cd命令用于切换当前工作目录至 dirName(目录参数). 其中 dirName 表示法可为绝对路径或相对路径.若目录名称省略,则变 ...
- AtCoder4351 Median of Medians 二分, 树状数组
题目大意 定义一个从小到大的数列的中位数为第 $ \frac{n}{2}+1 $ 项.求一个序列的所有连续子序列的中位数的中位数. $ (n \leqslant 100000)$ 问题分析 由于\(n ...
- hexo的流程
1.基于node 的npm包管理工具2.npm install -g hexo(hexo-cli)3.下载的资源是国外的可能比较慢,可以使用淘宝镜像(代理的仓库)npm install -g hexo ...
- Unity3D_(游戏)卡牌01_启动屏界面
卡牌2D游戏展示 (游戏代码放到 卡牌04_游戏界面 文章最后面~) 游戏项目已托管到github上(里面有个32bit可执行文件) 传送门 规则 开始游戏每张卡牌初始翻开展示 展示几秒后卡牌 ...