【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 ...
随机推荐
- WaitForSingleObject的作用[转]
在多线程的情况下,有时候我们会希望等待某一线程完成了再继续做其他事情(比如主线程等待子线程结束完之后,自己再结束),要实现这个目的,可以使用Windows API函数WaitForSingleObje ...
- node 添加个人经历的接口
1.定义experience const profileFields = {}; profileFields.experience=[]; 2.查找用户id const profile = await ...
- jQuery的ajaxFileUpload上传文件插件刷新一次才能再次调用触发change
jQuery的ajaxFileUpload插件 关于用ajaxfileupload时,遇到一个要刷新一次页面才能再次上传,用live()方法来绑定 file表单 的change事件就能够解决,直接$( ...
- TIM4定时器功能设置
一.初始化过程 /*********************************************************************** 利用TIM4定时器作为计时,每个0.1 ...
- c#获取MAC地址和IP地址
一获取mac地址 1.先添加system.management的dll组件2.添加引用 public string GetMACAddress(){string MoAddress = "& ...
- %各位大佬的博客.tql
线性基:https://www.cnblogs.com/ljh2000-jump/p/5869991.html#4219854 数位DP https://blog.csdn.net/jk211766 ...
- 2019 牛客暑期多校 第一场 H XOR (线性基)
题目:https://ac.nowcoder.com/acm/contest/881/H 题意:求一个集合内所有子集异或和为0的长度之和 思路:首先集合内异或和,这是线性基的一个明显标志,然后我们不管 ...
- 团队冲刺DAY4
DES算法 算法概要 在DES.java当中创立两个方法分别用作加密和解密 通过 `public static byte[] encrypt(byte[] data, String sKey) 创建方 ...
- ANSI 标准C 还定义了如下几个宏
ANSI 标准C 还定义了如下几个宏:_LINE_ 表示正在编译的文件的行号_FILE_ 表示正在编译的文件的名字预处理名称意义#define 宏定义#undef 撤销已定义过的宏名#include ...
- appium自动化测试- 元素操作
本文转自:https://www.cnblogs.com/sinder2018/articles/9699801.html 一.滑动屏幕 1.appium - 滑动屏幕 滑动接口: swipe(起始X ...