【leetcode】970. Powerful Integers
题目如下:
Given two non-negative integers
x
andy
, an integer is powerful if it is equal tox^i + y^j
for some integersi >= 0
andj >= 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^2Example 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等于1,那么幂值只会是1;如果x/y 大于1,由于 bound <= 10^6,幂的最大值是20(pow(2,20) > 10^6)。
代码如下:
class Solution(object):
def powerfulIntegers(self, x, y, bound):
"""
:type x: int
:type y: int
:type bound: int
:rtype: List[int]
"""
res = set()
x_max,y_max = 20 if x > 1 else 1,20 if y > 1 else 1
for i in range(x_max):
for j in range(y_max):
v = pow(x,i) + pow(y,j)
if v <= bound:
res.add(v)
return list(res)
【leetcode】970. Powerful Integers的更多相关文章
- 【LeetCode】970. Powerful Integers 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力搜索 日期 题目地址:https://leetc ...
- 【Leetcode_easy】970. Powerful Integers
problem 970. Powerful Integers solution: class Solution { public: vector<int> powerfulIntegers ...
- 【Leetcode】 - Divide Two Integers 位运算实现整数除法
实现两个整数的除法,不许用乘法.除法和求模.题目被贴上了BinarySearch,但我没理解为什么会和BinarySearch有关系.我想的方法也和BS一点关系都没有. 很早以前我就猜想,整数的乘法是 ...
- 【leetcode】Divide Two Integers (middle)☆
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- 【Leetcode】Divide Two Integers
Divide two integers without using multiplication, division and mod operator. class Solution { public ...
- 【leetcode】1291. Sequential Digits
题目如下: An integer has sequential digits if and only if each digit in the number is one more than the ...
- 【LeetCode】 454、四数之和 II
题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...
- 【LeetCode】18、四数之和
题目等级:4Sum(Medium) 题目描述: Given an array nums of n integers and an integer target, are there elements ...
- 【LeetCode】15、三数之和为0
题目等级:3Sum(Medium) 题目描述: Given an array nums of n integers, are there elements a, b, c in nums such t ...
随机推荐
- sass-loader屡次安装不生效的问题
报错信息: npm WARN sass-loader@8.0.0 requires a peer of webpack@^4.36.0 but none is installed. You must ...
- &与&&、|与||的区别
&和&& 相同之处: &和&&都表示:符号两端必须同时为真,最后的结果才为真:其中一端为假,则最后的结果为假 不同之处: &:左端为假,还需要继 ...
- 版本控制系统之SVN和GIT的区别
版本控制器的作用: 1. 可以协同代码管理,让多人开发代码得以实现. 2. 回归到以前的任何一个时间点的代码处(好比:开始写了很多代码,后面有修改了一些,突然IDE崩溃,但是发现还是以前的代码更好,这 ...
- 网站运行一段时间后就无法访问,重启Tomcat才能恢复
网站运行一段时间后就无法访问,重启Tomcat才能恢复出现这种情况,很可能是以下几种情况:1.超过数据库连接池上限2.并发数达到上限3.内存溢出具体还是需要通过打印的日志进行具体分析.解决方法1.如果 ...
- flask中的目录解析
首先我们看一下博客blog的封面,flask和django都属于web框架.不过flask属于轻量级web框架,功能的实现是通过扩展实现的,extension.py 中存放各个对象,具体的功能通过对象 ...
- python 使用yaml模块
python:yaml模块一.yaml文件介绍YAML是一种简洁的非标记语言.其以数据为中心,使用空白,缩进,分行组织数据,从而使得表示更加简洁.1. yaml文件规则基本规则: 大小写敏感 ...
- PHP面试 javascript和jQuery 基础
JavaScript基础 JavaScript语法 变量的定义:变量必须以字母开头 可以使用$和 _ 符号开头 变量名称对大小写敏感 使用 var 关键词声明变量 可以一次声明多个变量 ...
- processing模拟三角级数合成方波过程
代码 1: int radius = 2; 2: int[] accumys; 3: int times = 0; 4: 5: float scale = 1; 6: int origin = 400 ...
- layui点击图片放大-多图显示
layui点击图片放大-多图显示 标签(空格分隔): js HTML // div <div id="photo-list"> <img class=" ...
- QTP - excel操作
1. 以数据库的形式访问Excel 通常,我们与Excel的交互,是通过创建Excel对象的方式: Set ExcelApp = CreateObject("Excel.Applicatio ...