这是悦乐书的第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. 分布式中 CAP BASE ACID 理解(转载)

    概念理解(CAP,BASE, ACID) CAP CAP:  Consistency, Availability, Partition-tolerance 强一致性(Consistency).系统在执 ...

  2. springboot+elasticsearch 报错

    错误1: .d.e.r.s.AbstractElasticsearchRepository : failed to load elasticsearch nodes : org.elasticsear ...

  3. ab测试nginx Nginx性能优化

    转自:https://www.cnblogs.com/nulige/p/9369700.html 1.性能优化概述 在做性能优化前, 我们需要对如下进行考虑 1.当前系统结构瓶颈 观察指标 压力测试 ...

  4. beautifulsoup4 用法一二

    声明一个beautifulsoup4对象 bs = ( url,//路由 html_parser,//解析html代码 encoding//编码)//另一种请求解析方法 import requests ...

  5. PHP循环while do while循环

    <?php #从1打印到10,除了5 $i=1; while ($i<10) { if ($i==5) { $i++; continue; } echo $i++."<br ...

  6. 微信小程序没找到构建npm或者没找到node_modules目录以及如何在小程序中引入vant weapp组件

    微信小程序没找到构建npm或者没找到node_modules目录解决方法如下: 按照微信小程序提供的文档npm install是不行的,直接提示没找到可构建的npm包. 1.直接安装:npm init ...

  7. 21.django中间件源码阅读

    回顾: 关于里面的源码流程大家可以全看视频,因为代码的跳动性很大,而且会多次调用通过一方法,所以关于中间源码的部分去找个视频看一看,我写的不是很清楚. # 1 cookie session # 2 f ...

  8. 【java工具类】上传文件

    /**上传文件 * @param file 文件 * @param filePath 上传文件路径,不包含文件名 * @param fileName 新的文件名 * @return 返回一个路径名 * ...

  9. CF contest 1216 Div3. F

    题目链接:Click here Solution: 看起来是贪心,其实不然... 我们定义\(f[i]\)表示仅覆盖\(1\sim i\)所需要的最小代价,那么对\(i\)为0的点来说,易得\(f[i ...

  10. reactjs 的 css 模块化工具 styled-components 升级后 createGlobalStyle 废除,使用 createGlobalStyle 的方案

    在 styled-components 升级到 4 版本后设置全局属性的 createGlobalStyle 这个 api 被废除,替代的 api 是 createGlobalStyle 与过去组织代 ...