聚美优品的电子商务网站建设论文,淘宝购买网站建设,网站建设流程ppt,ui网上接单网站文章目录1. 题目2. 解题2.1 暴力法2.2 优化双重循环1. 题目
给定两个正整数 x 和 y#xff0c;如果某一整数等于 xi yj#xff0c;其中整数 i 0 且 j 0#xff0c;那么我们认为该整数是一个强整数。
返回值小于或等于 bound 的所有强整数组成的列表。
你可以按…
文章目录1. 题目2. 解题2.1 暴力法2.2 优化双重循环1. 题目
给定两个正整数 x 和 y如果某一整数等于 xi yj其中整数 i 0 且 j 0那么我们认为该整数是一个强整数。
返回值小于或等于 bound 的所有强整数组成的列表。
你可以按任何顺序返回答案。在你的回答中每个值最多出现一次。
示例 1
输入x 2, y 3, bound 10
输出[2,3,4,5,7,9,10]解释 2 20 30 3 21 30 4 20 31 5 21 31 7 22 31 9 23 30 10 20 32
示例 2
输入x 3, y 5, bound 15
输出[2,4,6,8,10,14]提示
1 x 100
1 y 100
0 bound 10^6来源力扣LeetCode 链接https://leetcode-cn.com/problems/powerful-integers 著作权归领扣网络所有。商业转载请联系官方授权非商业转载请注明出处。
2. 解题
2.1 暴力法
双重循环set去重
class Solution {
public:vectorint powerfulIntegers(int x, int y, int bound) {int i, j, res;unordered_setint s;int Maxi (x ! 1) ? log(bound)/log(x)1 : bound/2;int Maxj (y ! 1) ? log(bound)/log(y)1 : bound/2;for(i 0; i Maxi; i){for(j 0; j Maxj; j){res pow(x,i)pow(y,j);if(res bound)s.insert(res);}} return vectorint (s.begin(),s.end());}
};2.2 优化双重循环
优化x或者y等于1时的情况提前退出1的任何次方等于1只有一种选择
class Solution {
public:vectorint powerfulIntegers(int x, int y, int bound) {int i 0, j 0, res;unordered_setint s;int powx, powy;for(i 0; (powx pow(x,i)) bound; i){for(j 0; (powy pow(y,j)) bound; j){res powxpowy;if(res bound)s.insert(res);if(y 1)break;}if(x 1)break;} return vectorint (s.begin(),s.end());}
};or
class Solution {
public:vectorint powerfulIntegers(int x, int y, int bound) {int i, j, res;unordered_setint s;int Maxi (x ! 1) ? log(bound)/log(x)1 : 1;int Maxj (y ! 1) ? log(bound)/log(y)1 : 1;for(i 0; i Maxi; i){for(j 0; j Maxj; j){res pow(x,i)pow(y,j);if(res bound)s.insert(res);}} return vectorint (s.begin(),s.end());}
};