Leetcode970. Powerful Integers强整数
给定两个非负整数 x 和 y,如果某一整数等于 x^i + y^j,其中整数 i >= 0 且 j >= 0,那么我们认为该整数是一个强整数。
返回值小于或等于 bound 的所有强整数组成的列表。
你可以按任何顺序返回答案。在你的回答中,每个值最多出现一次。
示例 1:
输入: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:
输入:x = 3, y = 5, bound = 15 输出:[2,4,6,8,10,14]
提示:
- 1 <= x <= 100
- 1 <= y <= 100
- 0 <= bound <= 10^6
暴力法
如果枚举x的指数,那么每次选择指数后都要去得到x的幂运算结果。每次都进行幂运算,那么就重复了很多操作。所以先把幂运算的结果保存下来,去枚举幂运算的结果
class Solution {
public:
vector<int> powerfulIntegers(int x, int y, int bound)
{
vector<int> vx;
vector<int> vy;
vector<int> ans;
map<int, bool> check;
int tempx = 1;
int tempy = 1;
if(x == 1)
{
vx.push_back(1);
}
else
{
while(tempx <= bound)
{
vx.push_back(tempx);
tempx *= x;
}
}
if(y == 1)
{
vy.push_back(1);
}
else
{
while(tempy <= bound)
{
vy.push_back(tempy);
tempy *= y;
}
}
for(int i = 0; i < vx.size(); i++)
{
for(int j = 0; j < vy.size(); j++)
{
if(vx[i] + vy[j] <= bound && !check[vx[i] + vy[j]])
{
check[vx[i] + vy[j]] = 1;
ans.push_back(vx[i] + vy[j]);
}
}
}
return ans;
}
};
通过这道题突然想到了另外一道题,用的很简便神奇的方法。
题目描述
把只包含质因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含质因子7。 习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。
class Solution {
public:
int GetUglyNumber_Solution(int index) {
if(index <= 0)
return index;
vector<int> res(index);
res[0] = 1;
int t2 = 0, t3 = 0, t5 = 0;
for(int i = 1; i < index; i++)
{
res[i] = min(res[t2] * 2, min(res[t5] * 5, res[t3] * 3));
//不用else if的原因是为了去重
if(res[i] == res[t2] * 2)
t2++;
if(res[i] == res[t3] * 3)
t3++;
if(res[i] == res[t5] * 5)
t5++;
}
return res[index - 1];
}
};
Leetcode970. Powerful Integers强整数的更多相关文章
- 【LeetCode】Powerful Integers(强整数)
这道题是LeetCode里的第970道题. 题目描述: 给定两个正整数 x 和 y,如果某一整数等于 x^i + y^j,其中整数 i >= 0 且 j >= 0,那么我们认为该整数是一个 ...
- LeetCode 970. Powerful Integers (强整数)
题目标签:HashMap 题目让我们找出所有独一的powerful integers 小于bound的情况下. 把 x^i 看作 a:把 y^j 看作b, 代入for loop,把所有的情况都遍历一遍 ...
- [Swift]LeetCode970.强整数 | Powerful Integers
Given two non-negative integers x and y, an integer is powerful if it is equal to x^i + y^j for some ...
- LeetCode970. 强整数
问题:970. 强整数 用户通过次数0 用户尝试次数0 通过次数0 提交次数0 题目难度Easy 给定两个非负整数 x 和 y,如果某一整数等于 x^i + y^j,其中整数 i >= 0 且 ...
- Leetcode 970. 强整数
970. 强整数 显示英文描述 我的提交返回竞赛 用户通过次数223 用户尝试次数258 通过次数231 提交次数801 题目难度Easy 给定两个正整数 x 和 y,如果某一整数等于 x^i ...
- 118th LeetCode Weekly Contest Powerful Integers
Given two non-negative integers x and y, an integer is powerful if it is equal to x^i + y^j for some ...
- 【Leetcode_easy】970. Powerful Integers
problem 970. Powerful Integers solution: class Solution { public: vector<int> powerfulIntegers ...
- LC 970. Powerful Integers
Given two non-negative integers x and y, an integer is powerful if it is equal to x^i + y^j for some ...
- 【leetcode】970. Powerful Integers
题目如下: Given two non-negative integers x and y, an integer is powerful if it is equal to x^i + y^j fo ...
随机推荐
- NX二次开发-如何在类外面定义一个结构体
#include <uf.h> #include <uf_obj.h> #include <uf_part.h> using namespace NXOpen; u ...
- hibernate 一对多查询 对多的一方进行分页
//查询用户留言@Overridepublic List<LeaveWords> getLeaveWords(String userName) {Session session = nul ...
- Notepad++如何对比文件 Notepad++对比两个文件代码方法
大家在使用Notepad++的时候,需要对编辑的两个文件进行比较,找出两个文件代码的区别,快速进行编辑修改,那么Notepad++如何对比文件,下面小编就给大家带来Notepad++对比两个文件代码方 ...
- 创建 Angular 8.0 项目
创建 Angular 8.0 项目,首先确保已经安装了 nodejs,如果没有安装,请看这篇文章安装:node.js 安装 1.新建一个空文件夹 angularproject,作为工作区 2.安装 A ...
- C89,C99: C数组&结构体&联合体快速初始化
1. 背景 C89标准规定初始化语句的元素以固定顺序出现,该顺序即待初始化数组或结构体元素的定义顺序. C99标准新增指定初始化(Designated Initializer),即可按照任意顺序对数组 ...
- postman中如何传数组
方法一: postman的传参: java接收: package com.nps.base.xue.xd.groovyEngine import com.google.gson.Gson import ...
- 如何理解CUDA中的cudaMalloc()的参数
首先看下此运行时函数的原型: cudaError_t cudaMalloc (void **devPtr, size_t size ); 主要的第一个参数.为什么是两个星星呢?用个例子来说明下. fl ...
- Android读取logcat信息
测试的时候,经常遇到开发需要logcat分析定位bug,今天简单记录一下获取logcat的方法 前提条件:电脑中要安装好Android SDK 1.cmd 进入到这个界面 2.电脑连上手机,手机记得打 ...
- 【POJ】3268 Silver Cow Party
题目链接:http://poj.org/problem?id=3268 题意 :有N头奶牛,M条单向路.X奶牛开party,其他奶牛要去它那里.每头奶牛去完X那里还要返回.去回都是走的最短路.现在问这 ...
- Neo4j模糊查询及分页查询
Neo4j模糊查询:采用正则方式: MATCH (n:House) where n.Name =~ '李.*' RETURN n 分页: 使用skip 及 limit MATCH (n:House) ...