LeetCode970. 强整数

问题:970. 强整数

  • 用户通过次数0
  • 用户尝试次数0
  • 通过次数0
  • 提交次数0
  • 题目难度Easy

给定两个非负整数 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

链接:https://leetcode-cn.com/contest/weekly-contest-118/problems/powerful-integers/

分析:

数据范围并不大,只需要列出所有的Xs=X^i<=bound,Ys=Y^i<=bound,然后找到满足Xs+Ys<=bound即可。

 需要注意的有

1.如果x/等于1,对应的列表里面只会有1

2,有可能X^i11+Y^i12==X^i21+Y^i22,且i11!=i21,i12!=i22,比如2^4+4^0=2^0+4^2,所以需要对和去重。

AC Code:

 1 class Solution {
 2 public:
 3       vector<int> powerfulIntegers(int x, int y, int bound) {
 4         vector<int> ret;
 5         
 6         vector<int> xs;        
 7         vector<int> ys;
 8         int tmp = 0;
 9         while (true)
10         {
11             if (x == 1)
12             {
13                 xs.emplace_back(x);
14                 break;
15             }
16             int local = pow(x, tmp);
17             if (local > bound)
18             {
19                 break;
20             }
21             else
22             {
23                 xs.emplace_back(local);
24                 tmp++;
25             }
26         }
27 
28         tmp = 0;
29         while (true)
30         {
31             if (y == 1)
32             {
33                 ys.emplace_back(y);
34                 break;
35             }
36             int local = pow(y, tmp);
37             if (local > bound)
38             {
39                 break;
40             }
41             else
42             {
43                 ys.emplace_back(local);
44                 tmp++;
45             }
46         }
47         set<int> nums;    
48         for (int i = 0; i < xs.size(); i++)
49         {
50             for (int j = 0; j < ys.size(); j++)
51             {
52                 int local = xs[i] + ys[j];
53                 if (local > bound)
54                 {
55                     break;
56                 }
57                 else
58                 {
59                     nums.insert(local);
60                 }
61             }
62         }
63         for (set<int>::iterator it = nums.begin(); it != nums.end(); it++)
64         {
69                 ret.emplace_back(*it);
70         }
71         return ret;
72     }
73 
74 
75 };

其他:

国内第一code:

class Solution(object):
    def powerfulIntegers(self, x, y, bound):
        """
        :type x: int
        :type y: int
        :type bound: int
        :rtype: List[int]
        """
        if x > y:
            x, y = y, x
        ans = set()
        for i in range(20):
            for j in range(20):
                if x ** i + y ** j <= bound:
                    ans.add(x ** i + y ** j)
        return list(ans)

1<=x,y<=100,0<=bound<=10^6,由于x,y取整数,所以除1外最小的2^20=1048576>10^6,20*20两层循环即可,甚至可以在外层判断如果过大直接结束,内层也是如果大于bound可以直接结束。

如下:

class Solution {
public:
vector<int> powerfulIntegers(int x, int y, int bound) {
        vector<int> ret;
        if (x < y)
        {
            int tmp = x;
            x = y;
            y = tmp;
        }
        for(int i=0;i<20;i++)
        {
            int tmp = pow(x, i);
            if (tmp >= bound)
            {
                break;
            }
            for (int j = 0; j < 20; j++)
            {
                int tmpans =tmp+ pow(y, j);
                if (tmpans > bound)
                {
                    break;
                }
                else
                {
                    if (count(ret.begin(), ret.end(), tmpans) == 0)
                    {
                        ret.emplace_back(tmpans);
                    }
                }
            }
        }
        return ret;
    }
};

4ms战胜98.13% cpp code。

用时最短code:

class Solution {
public:
    vector<int> powerfulIntegers(int x, int y, int bound) {
       set<int> S;
        int i, tx, ty;
        for (tx = 1; tx <= bound; tx *= x) {
            for (ty = 1; ty <= bound; ty *= y) {
                if (tx + ty > bound)
                    break;
                S.insert(tx + ty);
                if (y == 1)
                    break;
            }
            if (x == 1)
                break;
        }
        auto it = S.begin();
        vector<int> ans;
        while (it != S.end()) {
            ans.push_back(*it);
            it++;
        }
        return ans;
        
    }
};

大概逻辑差不多,用set去重,计算幂的和,对比bound,对于1只用一次跳过循环

猜你喜欢

转载自www.cnblogs.com/youdias/p/10247208.html
今日推荐