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 ...
随机推荐
- 本地安装sass出错问题解析
2016年3月23日个人博客文章--迁移到segmentfault 安装sass ruby安装因为sass依赖于ruby环境,所以装sass之前先确认装了ruby.先导官网下载个ruby 在安装的时候 ...
- Acwing-282-石子合并(区间DP)
链接: https://www.acwing.com/problem/content/284/ 题意: 设有N堆石子排成一排,其编号为1,2,3,-,N. 每堆石子有一定的质量,可以用一个整数来描述, ...
- 04-01 Django之模板层
Django之模板层 一 模板简介 在刚刚介绍完的视图层中我们提到,浏览器发送的请求信息会转发给视图函数进行处理,而视图函数在经过一系列处理后必须要有返回信息给浏览器.如果我们要返回html标签.cs ...
- 前端有用JavaScript技巧
数组去重 //法1 var arr = [1, 2, 3, 3, 4]; console.log(...new Set(arr)) // [1, 2, 3, 4] //法2 function SetA ...
- jquery visible 选择器 语法
jquery visible 选择器 语法 作用::visible 选择器选取每个当前是可见的元素.除以下几种情况之外的元素即是可见元素:设置为 display:none type="h ...
- C# 时间格式转换
DateTime dd =DateTime.Parse(temp[0]); string aa = dd.ToShortDateString();
- vue自定义组件(vue.use(),install)+全局组件+局部组件
相信大家都用过element-ui.mintui.iview等诸如此类的组件库,具体用法请参考:https://www.cnblogs.com/wangtong111/p/11522520.html ...
- ES6指北【1】——let、const
1.如何学习ES6 1.1 js的学习顺序 ES5 -> ES6 -> ES7 -> ES8 以此类推 ES5没学好就别想学好ES6 1.2 边学边用 学了就要用 2.变量声明的方式 ...
- /static/fonts/SIMYOU.TTF’ 字体
/** * pdf 加水印 * * @return */ public byte[] pdfAddWaterMark(byte[] byes) { String fileName = UUID.ran ...
- OkHttp3 使用详解
一,简介 OkHttp 是一个高效的 HTTP 客户端,具有非常多的优势: 能够高效的执行 http,数据加载速度更快,更省流量 支持 GZIP 压缩,提升速度,节省流量 缓存响应数据,避免了重复的网 ...