作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/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. 1 <= x <= 100
  2. 1 <= y <= 100
  3. 0 <= bound <= 10^6

题目大意

给了两个非负整数x, y,问能组成的x^i + y^j <= bound有多少种。其中,i、j非负。

解题方法

暴力搜索

这个题是一个Easy题,在周赛做的时候,想复杂了,竟然想的是判断bound - x ^ i是不是y的幂。这样确实变复杂了,而且不好写。果断改成对i,j正向的两重循环。

想法是对i从0开始搜索,最多搜到bound,j也是最多搜到bound,判断得到的target = x^i + y^j 是不是<=bound,是的话就放入结果中,并且对j和i进行增长。由于题目要求不能重复,所以使用了set进行去重。

这个题坑爹的是当x或者y是1的时候,由于1的任何次方都是自己,所以有可能造成死循环,我就在这里卡住了。这也是为什么我限制了i和j是要递增的,并且它们的边界是bound的原因。

python代码如下:

class Solution(object):
def powerfulIntegers(self, x, y, bound):
"""
:type x: int
:type y: int
:type bound: int
:rtype: List[int]
"""
res = set()
i = 0
while x ** i <= bound and i <= bound:
j = 0
while j <= bound:
target = x ** i + y ** j
if target > bound:
break
res.add(target)
j += 1
i += 1
return list(res)

上面的做法虽然挺直白的,但是不够好,更好的想法是把xi和yj看成一个整体,这个整体每次自乘x或者y,然后判断两个整体相加的和是不是小于等于bound。同样地,也需要对x和y是不是1进行判断,这里的判断更容易一点,即如果等于1的话直接打破循环,不用再自乘x或者y了。

C++代码如下:

class Solution {
public:
vector<int> powerfulIntegers(int x, int y, int bound) {
set<int> res;
for (int i = 1; i < bound; i *= x) {
for (int j = 1; i + j <= bound; j *= y) {
res.insert(i + j);
if (y == 1) break;
}
if (x == 1) break;
}
return vector<int>(res.begin(), res.end());
}
};

参考资料:https://leetcode.com/problems/pancake-sorting/discuss/214213/JavaC%2B%2BPython-Straight-Forward

日期

2019 年 1 月 6 日 —— 打球打的腰酸背痛

【LeetCode】970. Powerful Integers 解题报告(Python & C++)的更多相关文章

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

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

  2. LeetCode: Divide Two Integers 解题报告

    Divide Two Integers Divide two integers without using multiplication, division and mod operator. SOL ...

  3. Leetcode 970. Powerful Integers

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

  4. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  5. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  6. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  7. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  8. 【Leetcode_easy】970. Powerful Integers

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

  9. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

随机推荐

  1. R语言与医学统计图形【6】低级绘图函数

    R语言基础绘图系统 基础绘图包之低级绘图函数--定义坐标轴.图例.文本 低级绘图函数:本身不具备图形绘制能力,只是在已有图形基础上添加元素. 函数 功能 arrows 添加箭头 axis 坐标轴 bo ...

  2. R语言与医学统计图形-【15】ggplot2几何对象之线图

    ggplot2绘图系统--几何对象之线图 曲线:点连线.路径曲线.时间序列曲线.模型拟合曲线...... 直线:水平直线.垂直直线.斜线. 1.曲线 对象及其参数. #路径图 geom_path(ma ...

  3. zabbix忘记密码——进入数据库修改

    登录数据库,选择zabbix数据库 查看数据库里面的表 用户和用户密码在users表里面 将你想设置的密码进行MD5加密处理: 更新密码即可: update users set passwd=&quo ...

  4. php-fpm一个PHPFastCGI进程管理器

    PHP-FPM(FastCGI Process Manager:FastCGI进程管理器)是一个PHPFastCGI管理器,对于PHP 5.3.3之前的php来说,是一个补丁包 [1]  ,旨在将Fa ...

  5. Oracle基础入门

    说明:钓鱼君昨天在网上找到一份oracle项目实战的文档,粗略看了一下大致内容,感觉自己很多知识不够扎实,便跟着文档敲了一遍,目前除了机械性代码没有实现外,主要涉及知识:创建表空间.创建用户.给用户赋 ...

  6. 联盛德 HLK-W806 (六): I2C驱动SSD1306 128x64 OLED液晶屏

    目录 联盛德 HLK-W806 (一): Ubuntu20.04下的开发环境配置, 编译和烧录说明 联盛德 HLK-W806 (二): Win10下的开发环境配置, 编译和烧录说明 联盛德 HLK-W ...

  7. c#图标、显示图表、图形、json echarts实例 数据封装【c#】

    page: <%@ Control Language="C#" AutoEventWireup="true" CodeFile="Viewxxx ...

  8. A Child's History of England.25

    It was a September morning, and the sun was rising, when the King was awakened from slumber by the s ...

  9. accelerate

    accelerate accelerare, accumulare和accurate共享一个含义为to的词根,后半截分别是:fast, pile up, care (关心则精确). 近/反义词: ex ...

  10. Scala【json字符串和json对象互相转换】

    一.fastjson工具 pom依赖 <dependency> <groupId>com.alibaba</groupId> <artifactId>f ...