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的更多相关文章

  1. 【Leetcode_easy】970. Powerful Integers

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

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

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

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

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

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

  5. Leetcode 970. Powerful Integers

    Brute Force(暴力) class Solution(object): def powerfulIntegers(self, x, y, bound): """ ...

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

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

  8. 【LeetCode】Powerful Integers(强整数)

    这道题是LeetCode里的第970道题. 题目描述: 给定两个正整数 x 和 y,如果某一整数等于 x^i + y^j,其中整数 i >= 0 且 j >= 0,那么我们认为该整数是一个 ...

  9. LeetCode.970-强大的整数(Powerful Integers)

    这是悦乐书的第367次更新,第395篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第229题(顺位题号是970).给定两个正整数x和y,如果对于某些整数i >= 0 ...

随机推荐

  1. jmeter连接mysql数据库进行多条语句查询

    前提工作: 1.在jmeter官网下载jmeter包(官网地址:https://jmeter.apache.org/).此外还需下载mysql驱动包,如:mysql-connector-java-5. ...

  2. CDH5.16.1的kafka集群报错: No broker in ISR for partition [getngo_collect_apm_test,0]

    1 详细错误 kafka.common.NoReplicaOnlineException: No broker in ISR for partition [getngo_collect_apm_tes ...

  3. Django项目实战—用户头像上传

    1 将文件保存到服务器本地 upload.html <!DOCTYPE html> <html lang="en"> <head> <me ...

  4. 2.Bacula Server端安装配置

    1.  Bacula Server端安装配置 1.1.  Bacula Server端安装 1.1.1.  安装bacula依赖包 For Centos6: yum install -y mysql ...

  5. svn中日志不展示解决方法记录

    一,问题:点击svn查看日志时不显示.筛选时间显示为1970 1,猜想可能没有查看日志权限 2,查看linux 下svn版本库 confg 下三个配制文件 authz ,passwd ,svnserv ...

  6. MyBatis-08-使用注解开发

    8.使用注解开发 8.1.面向接口编程 面向接口编程的根本原因:解耦,可拓展,提高复用,分层开发中.上层不用管具体的实现,大家都遵守共同的标准,使得开发变得容易,规范性好 8.2.使用注解开发 注解在 ...

  7. CSCD核心,北大中文核心

    从两篇文章看两个杂志 title 子空间聚类的重建模型及其快速算法 稀疏正则非凸优化问题之全局收敛分析 author 夏雨晴(浙江大学数学科学学院),张振跃 储敏(武汉大学数学与统计学院) journ ...

  8. Hivesql中的正则

    ================================================================================================= 一般 ...

  9. Codeforces Round #590 (Div. 3) A. Equalize Prices Again

    链接: https://codeforces.com/contest/1234/problem/A 题意: You are both a shop keeper and a shop assistan ...

  10. 【细谈Java并发】谈谈LinkedBlockingQueue(转)

    最近在看concurrent包的知识,看到LinkedBlockingQueue,发现一篇好文推荐给大家.原文地址:[细谈Java并发]谈谈LinkedBlockingQueue 1.简介 上篇我们介 ...