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 integers i >= 0
and j >= 0
.
Return a list of all powerful integers that have value less than or equal to bound
.
You may return the answer in any order. In your answer, each value should occur at most once.
Example 1:
Input: x = 2, y = 3, bound = 10
Output: [2,3,4,5,7,9,10]
Explanation:
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
Example 2:
Input: x = 3, y = 5, bound = 15
Output: [2,4,6,8,10,14]
Note:
1 <= x <= 100
1 <= y <= 100
0 <= bound <= 10^6
Runtime: 9 ms, faster than 52.02% of Java online submissions for Powerful Integers.
class Solution {
public static void init(List<Integer> A, int x, int bound){
if(x == ){
A.add();
return;
}
for(int i=; ; i++){
int tmp = (int) Math.pow(x, i);
if(tmp < bound){
A.add(tmp);
}else break;
}
return ;
}
public List<Integer> powerfulIntegers(int x, int y, int bound) {
List<Integer> arrx = new ArrayList<>();
List<Integer> arry = new ArrayList<>();
init(arrx, x, bound);
init(arry, y, bound);
Set<Integer> s = new HashSet<>();
for(int i=; i<arrx.size(); i++){
for(int j=; j<arry.size(); j++){
if(arrx.get(i) + arry.get(j) <= bound) s.add(arrx.get(i) + arry.get(j));
}
}
List<Integer> ret = new ArrayList<>();
Iterator<Integer> iter = s.iterator();
while(iter.hasNext()){
ret.add(iter.next());
}
return ret;
}
}
LC 970. Powerful Integers的更多相关文章
- 【Leetcode_easy】970. Powerful Integers
problem 970. Powerful Integers solution: class Solution { public: vector<int> powerfulIntegers ...
- LeetCode 970. Powerful Integers (强整数)
题目标签:HashMap 题目让我们找出所有独一的powerful integers 小于bound的情况下. 把 x^i 看作 a:把 y^j 看作b, 代入for loop,把所有的情况都遍历一遍 ...
- 【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】970. Powerful Integers 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力搜索 日期 题目地址:https://leetc ...
- Leetcode 970. Powerful Integers
Brute Force(暴力) class Solution(object): def powerfulIntegers(self, x, y, 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 ...
- 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】Powerful Integers(强整数)
这道题是LeetCode里的第970道题. 题目描述: 给定两个正整数 x 和 y,如果某一整数等于 x^i + y^j,其中整数 i >= 0 且 j >= 0,那么我们认为该整数是一个 ...
- LeetCode.970-强大的整数(Powerful Integers)
这是悦乐书的第367次更新,第395篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第229题(顺位题号是970).给定两个正整数x和y,如果对于某些整数i >= 0 ...
随机推荐
- idea代码爆红,clean,或者maven reimport都不起作用
1 突然自己的idea的Maven项目代码都是爆红,但是可以运行,添加新的代码确无法运行 尝试了clean,或者reimport,甚至是大家推荐的,刷新缓存重启也没有作用 2 检查项目的jdk配置,也 ...
- 五:MVC使用数据库优先(DatabaseFirst)的方式创建数据模型
1. ORM概念 2. EF的DatabaseFirst模式使用 1. ORM简介 对象关系映射(Object Relational Mapping,简称ORM) ORM技术特点: 1.提高了开发效率 ...
- Hadoop_18_MapRduce 内部的shuffle机制
1.Mapreduce的shuffle机制: Mapreduce中,map阶段处理的数据如何传递给Reduce阶段,是mapreduce框架中最关键的一个流程,这个流程就叫shuffle 将mapta ...
- delphi xe5 fastreport4.14 中文很多时换行不正确
用一般的frxMEMOview 中文换行是瞎换,缺少数据,换成frxrichview 即可, frxrichview 使用注意点 1).Delphi中文很多时换行不正确 2).要在窗体上拖一个frxr ...
- Build with runtime packages
编译问题:为什么我去掉Build with runtime packages,编译没问题??? 如果不去掉,就有错误:[Linker Error] Unresolved external 'TXNet ...
- Redis入门(一)
Redis入门 什么是Redis Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库,属于非关系型数据库的一种(NoSQL). Redis 与其他 key - val ...
- Maven打Dubbo可执行Jar
POM文件中添加如下配置: <build> <finalName>test-jar</finalName> <resources> <resour ...
- [转]Normal Map中的值, Tangent Space, 求算 Tangent 与 Binormal 与 TBN Matrix
原文出处 https://www.cnblogs.com/lookof/p/3509970.html - Normal Map中的值 - 有没有想过,Normal Map(法线贴图)为什么看上去都 ...
- Linux查找文件内容小技巧
目录 grep ag linux系统查找文件内容最常见的命令有grep和ag grep grep是比较常见的查找命令 # 在当前目录的py文件里查找所有相关内容 grep -a "broad ...
- BZOJ 4260: Codechef REBXOR (trie树维护异或最大值)
题意 分析 将区间异或和转化为前缀异或和.那么[L,R][L,R][L,R]的异或和就等于presum[R] xor presum[L−1]presum[R]\ xor \ presum[L-1]pr ...