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. 数学模块 math 函数的调用

    数学模块 math 模块名: math 注: linux下为内建模块 Mac OS下为标准库模块 数学模块用法: import math # 或 from math import * 数据 描述 ma ...

  2. 五:MVC使用数据库优先(DatabaseFirst)的方式创建数据模型

    1. ORM概念 2. EF的DatabaseFirst模式使用 1. ORM简介 对象关系映射(Object Relational Mapping,简称ORM) ORM技术特点: 1.提高了开发效率 ...

  3. 使用Response下载(支持任何格式)

    使用Response下载 下面代码: protected void Button2_Click(object sender, EventArgs e) { Response.ContentType = ...

  4. CentOS7.x忘记root密码如何破解

    在CentOS7.x中,有一个单用户模式.CentOS7.x进入单用户模式与CentOS6.x略有不同,要复杂一些. 如果我们忘记了root的密码,可以在单用户模式下重置密码. 注意:此操作必须在服务 ...

  5. 如何避免重复安装AppiumSetting、Unlock以及Android ime

    老版本Appium(如1.4.16),找到如下路径(根据自己的安装路径找) C:\Program Files (x86)\Appium\node_modules\appium\lib\devices\ ...

  6. python Pillow 图片处理模块,好强大有没有

    python Pillow 图片处理模块,好强大有没有 Pillow 需要给 python 另外安装 第一个用法:https://www.cnblogs.com/ibingshan/p/1105739 ...

  7. 微信小程序 getSystemInfoSync

    getSystemInfoSync 获取用户设备的相关信息 示例代码: 使用位置:在 JS文件的任意函数中使用 const res = wx.getSystemInfoSync(); console. ...

  8. metal sample code

    https://developer.apple.com/metal/sample-code/ 又被我发现了个宝贝

  9. 《SVG精髓》笔记(一)

    本文是基于<SVG精髓>一书的简单总结,文中的demo均为该书提供,目的是方便大家使用时快速查阅. 1. 坐标系统 视口(viewport):文档使用的画布区域,表示SVG可见区域的大小, ...

  10. Jquery 前端解码base64出现中文乱码的问题解决方案

    <script src="../static/js/jquery.base64.js"></script> <script src="../ ...