1. 程式人生 > >LeetCode970. 強整數

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只用一次跳過迴圈