【leetcode】970. Powerful Integers
题目如下:
Given two non-negative integers
xandy, an integer is powerful if it is equal tox^i + y^jfor some integersi >= 0andj >= 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 <= 1001 <= y <= 1000 <= 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 ...
随机推荐
- python 中字符串中含变量方法
1. 简单粗鲁的字符串拼接 1 name = "abc" 2 age = 25 3 info = "the name is "+name +"\nth ...
- leetcode-第14周双周赛-1273-删除树节点
题目描述: 自己的提交:动态规划 class Solution: def deleteTreeNodes(self, nodes: int, parent: List[int], value: Lis ...
- openwrt增加密码及ssh的方法
openwrt增加密码及ssh的方法 1.进入openwrt系统源码的顶层目录,然后执行 make menuconfig命令进入 Network--> SSH--> <*>o ...
- table 中 当前行变量的获取
- mysql inner join用法
inner join(等值连接):只返回两个表中联结字段相等的行. left join(左联接):返回包括左表中的所有记录和右表中联结字段相等的记录. right join(右联接):返回包括右表中的 ...
- vue中使用iview表单验证时this指针问题
需求 使用iview,在提交时对值b进行验证,使其不能大于值a 实现 <Form ref="config" :model="config" :rules= ...
- OC学习篇之---类的三大特性(封装,继承,多态)
之前的一片文章介绍了OC中类的初始化方法和点语法的使用:http://blog.csdn.net/jiangwei0910410003/article/details/41683873,今天来继续学习 ...
- SLA(服务等级协议)
SLA:Service-Level Agreement的缩写,意思是服务等级协议.是关于网络服务供应商和客户间的一份合同,其中定义了服务类型.服务质量和客户付款等术语. 定义SLA:Service-L ...
- 双目立体匹配经典算法之Semi-Global Matching(SGM)概述:匹配代价计算之互信息(Mutual Information,MI)
半全局立体匹配算法Semi-Global Matching,SGM由学者Hirschmüller在2005年所提出1,提出的背景是一方面高效率的局部算法由于所基于的局部窗口视差相同的假设在很多情况 ...
- 这里ajax需要改成同步
var flag = true; var title = $("#modal").find("input[name=groupname]").val(); /* ...