Leetcode 970. Powerful Integers

Brute Force(暴力)

class Solution(object):
    def powerfulIntegers(self, x, y, bound):
        """
        :type x: int
        :type y: int
        :type bound: int
        :rtype: List[int]
        """
        ans=[]

        for i in range(20):
            for j in range(20):
                a=x**i+y**j
                if a<=bound:
                    ans.append(a)

        return list(set(ans))

原文地址:https://www.cnblogs.com/zywscq/p/10540582.html

时间: 2024-10-11 03:47:20

Leetcode 970. Powerful Integers的相关文章

【Leetcode_easy】970. Powerful Integers

problem 970. Powerful Integers solution: class Solution { public: vector<int> powerfulIntegers(int x, int y, int bound) { unordered_set<int> tmp; for(int a=1; a<bound; a*=x)// { for(int b=1; a+b<=bound; b*=y) { tmp.insert(a+b);// if(y==1

LeetCode: Divide Two Integers [028]

[题目] Divide two integers without using multiplication, division and mod operator. [题意] 计算两个数的商,不能使用乘.除.取余操作 [思路] 用加法,确定多少除数相加其和恰好<=被除数 为了提高算法效率,利用贪心思想,采用滚雪球式的翻倍叠加策略,使和快速逼近被除数 几种特殊情况需要注意: 1. 结果是负数 2. 除数的绝对值要比被除数的绝对值大 3. 除数是0 4. 被除是0 5. 注意除数翻倍累加时越界,超过i

leetcode——Divide Two Integers 不用乘除取余操作求除法(AC)

Divide two integers without using multiplication, division and mod operator. 题目只有简单的一句话,看起来可真简单啊,呵呵,假象.这个题目的难点在于对时间效率的限制和边界值的测试.第一印象肯定是循环一个个把因子从被除数中减去不久行了么,可是对于比如INT_MAX/1或者INT_MIN/1之类的执行时间长的可怕,会超出时间限制.改善时间效率的思路是参考网上别人代码,将因子不断乘以2(可以通过移位实现,同时结果也从1开始不断

[Swift Weekly Contest 118]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 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 ord

LeetCode | 1387. Sort Integers by The Power Value将整数按权重排序【Python】

LeetCode 1387. Sort Integers by The Power Value将整数按权重排序[Medium][Python][排序] Problem LeetCode The power of an integer x is defined as the number of steps needed to transform x into 1 using the following steps: if x is even then x = x / 2 if x is odd t

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

这是悦乐书的第367次更新,第395篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第229题(顺位题号是970).给定两个正整数x和y,如果对于某些整数i >= 0且j >= 0等于x^i + y^j,则整数是强大的. 返回值小于或等于bound的所有强大整数的列表. 你可以按任何顺序返回答案.在你的答案中,每个值最多应出现一次.例如: 输入:x = 2,y = 3,bound = 10 输出:[2,3,4,5,7,9,10] 说明: 2 = 2^0 + 3^0

[LeetCode] Divide Two Integers

In this problem, we are asked to divide two integers. However, we are not allowed to use division, multiplication and mod operations. So, what else can be use? Well, bit manipulations. Let's look at an example and see how bit manipulation might help.

LeetCode: Divide Two Integers 解题报告

Divide Two Integers Divide two integers without using multiplication, division and mod operator. SOLUTION 11. 基本思想是不断地减掉除数,直到为0为止.但是这样会太慢. 2. 我们可以使用2分法来加速这个过程.不断对除数*2,直到它比被除数还大为止.加倍的同时,也记录下cnt,将被除数减掉加倍后的值,并且结果+cnt. 因为是2倍地加大,所以速度会很快,指数级的速度. 3. 另外要注意的是

[LeetCode] Divide Two Integers( bit + 二分法 )

Divide two integers without using multiplication, division and mod operator. 常常出现大的负数,无法用abs()转换成正数的情况 class Solution{ private: vector<long long> f; public: int bsearch(vector<long long> &a,int left,int right,long long key){ if(left > r