【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 ...
随机推荐
- Zip函数(Python)
>>> z = zip((2,3,4),(33,44,55)) >>> z <zip object at 0x1022cdb88> >>&g ...
- 深度 | 带领国产数据库走向世界,POLARDB底层逻辑是什么?
POLARDB 是阿里云自主研发的下一代云原生分布式数据库,100%兼容MySQL.PostgreSQL等开源数据库,高度兼容Oracle语法,使用RDS服务的客户不需要修改应用代码,可以一键迁移到P ...
- UNP学习第七章
一.套接口选项 函数getsockopt和setsockopt 函数fcntl 函数ioctl 二.getsockopt和setsockopt函数 #include <sys/socket.h& ...
- AcWing 230. 排列计数 水题(组合数+错排)打卡
题目:https://www.acwing.com/problem/content/232/ #include<bits/stdc++.h> #define ll long long #d ...
- JavaWeb解决中文乱码
1.Get请求,方案有两种 A:修改Tomcat配置文件 server.xml URIEncoding="UTF-8" 如:<Connector port="8 ...
- 尚学linux课程---11、vim操作命令1
尚学linux课程---11.vim操作命令1 一.总结 一句话总结: 要看不同的视频,每个视频的关键点都不一样,不如之间的的视频就没讲到vim中set nu是什么意思 1.Vi有三种基本工作模式? ...
- PAT 1042 Shuffling Machine (20 分)
1042 Shuffling Machine (20 分) Shuffling is a procedure used to randomize a deck of playing cards. ...
- jquery给表格绑值
jquery给表格绑值 直接上代码了 <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ...
- layui.form小例子
layui.form小例子 需要引入layui的包 <!doctype html> <html> <head> <meta charset="utf ...
- yum安装apache
一.查询是否已经安装apache rpm -qa httpd 注:Apache在linux系统里的名字是httpd 如果有返回的信息,则会显示已经安装的软件.如果没有则不会显示其它的信息.如下图是 ...