Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >【leetcode刷题】T55-强整数

【leetcode刷题】T55-强整数

作者头像
木又AI帮
修改于 2019-07-18 02:08:25
修改于 2019-07-18 02:08:25
43900
代码可运行
举报
文章被收录于专栏:木又AI帮木又AI帮
运行总次数:0
代码可运行

【题目】

给定两个正整数 xy,如果某一整数等于 x^i + y^j,其中整数 i >= 0j >= 0,那么我们认为该整数是一个强整数

返回值小于或等于 bound 的所有强整数组成的列表。

你可以按任何顺序返回答案。在你的回答中,每个值最多出现一次。

示例 1:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
输入:x = 2, y = 3, bound = 10
输出:[2,3,4,5,7,9,10]
解释: 
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

示例 2:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
输入:x = 3, y = 5, bound = 15
输出:[2,4,6,8,10,14]

提示:

  • 1 <= x <= 100
  • 1 <= y <= 100
  • 0 <= bound <= 10^6

【思路】

本题暴力破解即可,首先得到x和y的所有指数结果lsx和lsy,接着将lsx和lsy的元素分别相加,最后取得唯一集合。

【代码】

python版本

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class Solution(object):
    def get_all_num(self, x, bound):
        ls = []
        if x == :
            return ls
        num = x
        while num < bound:
            ls.append(num)
            num *= x
        return ls
    
    def powerfulIntegers(self, x, y, bound):
        """
        :type x: int
        :type y: int
        :type bound: int
        :rtype: List[int]
        """
        ls1 = self.get_all_num(x, bound)
        ls2 = self.get_all_num(y, bound)
        res = []
        for ls1i in ls1:
            for ls2i in ls2:
                tmp = ls1i + ls2i
                if tmp > bound:
                    break
                res.append(tmp)
        return list(set(res))

C++版本

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class Solution {
public:
    vector<int> get_all_num(int x, int bound){
        vector<int> ls(,);
        if(x == )
            return ls;
        int num = x;
        while(num < bound){
            ls.push_back(num);
            num *= x;
        }
        return ls;
    }
    vector<int> powerfulIntegers(int x, int y, int bound) {
        vector<int> ls1 = get_all_num(x, bound);
        vector<int> ls2 = get_all_num(y, bound);
        map<int, int> d;
        int tmp;
        for(auto n1: ls1){
            for(auto n2:ls2){
               tmp = n1 + n2;
                if(tmp > bound)
                    break;
                d[tmp] = ;
            }
        }
        vector<int> res;
        for(map<int, int>::iterator it=d.begin(); it != d.end(); it++)
            res.push_back(it->first);
        return res;
    }
};
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-05-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 木又AI帮 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
【leetcode刷题】T119-二叉树的层次遍历 II
https://leetcode-cn.com/problems/binary-tree-level-order-traversal-ii/
木又AI帮
2019/07/22
2850
【leetcode刷题】T93-最常见的单词
给定一个段落 (paragraph) 和一个禁用单词列表 (banned)。返回出现次数最多,同时不在禁用列表中的单词。题目保证至少有一个词不在禁用列表中,而且答案唯一。
木又AI帮
2019/07/17
5130
【leetcode刷题】T53-错误的集合
集合 S 包含从1到 n 的整数。不幸的是,因为数据错误,导致集合里面某一个元素复制了成了集合里面的另外一个元素的值,导致集合丢失了一个整数并且有一个元素重复。
木又AI帮
2019/07/17
2800
【leetcode刷题】T34-只出现一次的数字 II
Given a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that single one.
木又AI帮
2019/07/17
3100
【leetcode刷题】T67-字母异位词分组
给定一个字符串数组,将字母异位词组合在一起。字母异位词指字母相同,但排列不同的字符串。
木又AI帮
2019/07/17
3310
【leetcode刷题】T46-丑数 II
Write a program to find the n-th ugly number.
木又AI帮
2019/07/17
3670
【leetcode刷题】T56-重复的DNA序列
所有 DNA 由一系列缩写为 A,C,G 和 T 的核苷酸组成,例如:“ACGAATTCCG”。在研究 DNA 时,识别 DNA 中的重复序列有时会对研究非常有帮助。
木又AI帮
2019/07/17
5710
【leetcode刷题】T57-两个列表的最小索引总和
假设Andy和Doris想在晚餐时选择一家餐厅,并且他们都有一个表示最喜爱餐厅的列表,每个餐厅的名字用字符串表示。
木又AI帮
2019/07/17
6670
【leetcode刷题】T40-根据字符出现频率排序
Given a string, sort it in decreasing order based on the frequency of characters.
木又AI帮
2019/07/17
6200
【leetcode刷题】20T17-下一个排列
https://leetcode-cn.com/problems/next-permutation/
木又AI帮
2020/02/19
3130
【leetcode刷题】T47-超级丑数
Write a program to find the nth super ugly number.
木又AI帮
2019/07/17
4090
【leetcode刷题】T137-出现次数最多的子树元素和
https://leetcode-cn.com/problems/most-frequent-subtree-sum/
木又AI帮
2019/08/12
4020
【leetcode刷题】T133-二叉树的所有路径
https://leetcode-cn.com/problems/binary-tree-paths/
木又AI帮
2019/08/06
4600
【leetcode刷题】T33-只出现一次的数字
Given a non-empty array of integers, every element appears twice except for one. Find that single one.
木又AI帮
2019/07/17
3630
1062 最简分数 (20 分)
一个分数一般写成两个整数相除的形式:N/M,其中 M 不为0。最简分数是指分子和分母没有公约数的分数表示形式。
可爱见见
2019/10/25
4320
【leetcode刷题】20T45-格雷编码
https://leetcode-cn.com/problems/gray-code/
木又AI帮
2020/05/11
3270
【leetcode刷题】T140-二叉搜索树的最小绝对差
https://leetcode-cn.com/problems/minimum-absolute-difference-in-bst/
木又AI帮
2019/08/12
4900
【leetcode刷题】T25-下一个更大元素 I
You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nums1's elements in the corresponding places of nums2.
木又AI帮
2019/07/17
4520
【leetcode刷题】T49-键盘行
Given a List of words, return the words that can be typed using letters of alphabet on only one row's of American keyboard like the image below.
木又AI帮
2019/07/17
4040
【leetcode刷题】T49-键盘行
【leetcode刷题】T127-二叉树的后序遍历
https://leetcode-cn.com/problems/binary-tree-postorder-traversal/
木又AI帮
2019/07/30
3610
相关推荐
【leetcode刷题】T119-二叉树的层次遍历 II
更多 >
领券
社区富文本编辑器全新改版!诚邀体验~
全新交互,全新视觉,新增快捷键、悬浮工具栏、高亮块等功能并同时优化现有功能,全面提升创作效率和体验
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验