【LeetCode】970. Powerful Integers 解题报告(Python & C++)
作者: 负雪明烛
 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 <= x <= 100
 - 1 <= y <= 100
 - 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++)的更多相关文章
- LeetCode 970. Powerful Integers (强整数)
		
题目标签:HashMap 题目让我们找出所有独一的powerful integers 小于bound的情况下. 把 x^i 看作 a:把 y^j 看作b, 代入for loop,把所有的情况都遍历一遍 ...
 - LeetCode: Divide Two Integers 解题报告
		
Divide Two Integers Divide two integers without using multiplication, division and mod operator. SOL ...
 - Leetcode 970. Powerful Integers
		
Brute Force(暴力) class Solution(object): def powerfulIntegers(self, x, y, bound): """ ...
 - 【LeetCode】120. Triangle 解题报告(Python)
		
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
 - LeetCode 1 Two Sum 解题报告
		
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
 - 【LeetCode】Largest Number 解题报告
		
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
 - 【LeetCode】Permutations II 解题报告
		
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
 - 【Leetcode_easy】970. Powerful Integers
		
problem 970. Powerful Integers solution: class Solution { public: vector<int> powerfulIntegers ...
 - 【LeetCode】Island Perimeter 解题报告
		
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
 
随机推荐
- ggplot 画堆叠柱状图
			
1. 关注下方公众号可获得更多精彩
 - nginx_rewrite
			
介绍: 和apache等web服务软件一样,rewrite的组要功能是实现RUL地址的重定向.Nginx的rewrite功能需要PCRE软件的支持,即通过perl兼容正则表达式语句进行规则匹配的.默认 ...
 - error while loading shared libraries: libstdc++.so.5: wrong ELF class: ELFCLASS64
			
今天部署一个探针在运行的时候报了这样一个错:error while loading shared libraries: libstdc++.so.5: wrong ELF class: ELFCLAS ...
 - nginx二级域名指向不同文件项目配置
			
需要使用泛域名解析, 并且加上空的判断,以保证没有二级域名的也可以访问 核心配置 server_name ~^(?<subdomain>.+)\.caipudq\.cn$;if ( $su ...
 - 用原生CSS编写-怦怦跳的心
			
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
 - Hadoop入门 集群时间同步
			
集群时间同步 如果服务器在公网环境(能连接外网),可以不采用集群时间同步.因为服务器会定期和公网时间进行校准. 如果服务器在内网环境,必须要配置集群时间同步,否则时间久了,会产生时间偏差,导致集群执行 ...
 - acclaim
			
欲学acclaim,先学claim.即使学会claim,未必记住acclaim. [LDOCE] claim的词源是cry out, shoutverb:1. state that sth is tr ...
 - deque、queue和stack深度探索(下)
			
deque如何模拟连续空间?通过源码可以看到这个模型就是通过迭代器来完成. 迭代器通过重载操作符+,-,++,--,*和->来实现deque连续的假象,如上图中的 finish-start ,它 ...
 - Linux基础命令---mysqladmin数据库管理工具
			
mysqladmin mysqladmin是mysql数据库的管理工具,可以控制.查看.修改数据库服务器的配置和状态. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.Fedor ...
 - Can we call an undeclared function in C++?
			
Calling an undeclared function is poor style in C (See this) and illegal in C++. So is passing argum ...