这是悦乐书的第367次更新,第395篇原创

01 看题和准备

今天介绍的是LeetCode算法题中Easy级别的第229题(顺位题号是970)。给定两个正整数xy,如果对于某些整数i >= 0j >= 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)的更多相关文章

  1. [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 ...

  2. LeetCode 970. Powerful Integers (强整数)

    题目标签:HashMap 题目让我们找出所有独一的powerful integers 小于bound的情况下. 把 x^i 看作 a:把 y^j 看作b, 代入for loop,把所有的情况都遍历一遍 ...

  3. 【LeetCode】970. Powerful Integers 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力搜索 日期 题目地址:https://leetc ...

  4. 【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 ...

  5. 【Leetcode_easy】970. Powerful Integers

    problem 970. Powerful Integers solution: class Solution { public: vector<int> powerfulIntegers ...

  6. 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 ...

  7. 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 ...

  8. C#版 - Leetcode 13. 罗马数字转整数 - 题解

    C#版 - Leetcode 13. 罗马数字转整数 - 题解 Leetcode 13. Roman to Integer 在线提交: https://leetcode.com/problems/ro ...

  9. 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 ...

随机推荐

  1. MySQL第二次安装随笔

    找到之前的MySQL的安装包,重新安装MySQL. 1.设置环境变量,win10的可以右键此电脑-属性,在系统变量Path中添加mysql文件bin的路径 2.修改配置文件mydefault.ini( ...

  2. js 点击不同li 切换颜色

    html <div> <li " onclick='addColor(this.id)'> 会议简介 </li> <li " onclic ...

  3. [Go] 使用读写锁对map资源进行安全处理

    当需要有一个全局性的map集合资源进行增删改数据时,需要对该map资源增加读写锁,防止并发时出现安全问题 下面的类就是举例 , 属性中的Conns模拟存储一些资源,对这些资源进行并发的增加数据,使用写 ...

  4. java8 base64编码和解码

    package com.oy; import java.nio.charset.StandardCharsets; import java.util.Base64; import org.junit. ...

  5. shiro之缓存

    1 细说shiro之七:缓存:https://www.cnblogs.com/nuccch/p/8044226.html 2 Shiro缓存使用Redis.Ehcache.自带的MpCache实现的三 ...

  6. 计算机网络(二),TCP/IP四层模型常见协议

    目录 1.应用层协议 2.传输层协议 3.网络层协议 4.链路层协议 二.TCP/IP四层模型常见协议 1.应用层协议 (1)POP3 (2)FTP (3)HTTP (4)Telnet (5)SMTP ...

  7. windows下安装geopandas

    在linux下没任何问题,直接 pipenv install geopandas, 各种依赖库该编译的编译,该依赖的依赖. win下pyproj4编译不过,而且gdal库什么的也都是问题,建议全部从h ...

  8. 哈密尔顿环x

    欧拉回路是指不重复地走过所有路径的回路,而哈密尔顿环是指不重复地走过所有的点,并且最后还能回到起点的回路.  代码如下: #include<iostream> #include<cs ...

  9. Miller Rabin 算法简介

    0.1 一些闲话 最近一次更新是在2019年11月12日.之前的文章有很多问题:当我把我的代码交到LOJ上,发现只有60多分.我调了一个晚上,尝试用{2, 3, 5, 7, 11, 13, 17, 1 ...

  10. 【BZOJ4456】 [Zjoi2016]旅行者 / 【UOJ #184】 【ZJOI2016】旅行者

    Description 小Y来到了一个新的城市旅行.她发现了这个城市的布局是网格状的,也就是有n条从东到西的道路和m条从南到北 的道路,这些道路两两相交形成n×m个路口 (i,j)(1≤i≤n,1≤j ...