[Swift]LeetCode970.強整數 | Powerful Integers

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-csihrhdh-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

Given two non-negative integers x and y, an integer is powerful if it is equal to x^i + y^j for some integers i >= 0 and j >= 0.git

Return a list of all powerful integers that have value less than or equal to bound.github

You may return the answer in any order.  In your answer, each value should occur at most once.微信

Example 1:app

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^2 

Example 2:less

Input: x = 3, y = 5, bound = 15 Output: [2,4,6,8,10,14]

Note:spa

  • 1 <= x <= 100
  • 1 <= y <= 100
  • 0 <= bound <= 10^6

給定兩個非負整數 x 和 y,若是某一整數等於 x^i + y^j,其中整數 i >= 0 且 j >= 0,那麼咱們認爲該整數是一個強整數code

返回值小於或等於 bound 的全部強整數組成的列表。htm

你能夠按任何順序返回答案。在你的回答中,每一個值最多出現一次。blog

示例 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

 8ms

 1 class Solution {
 2     func powerfulIntegers(_ x: Int, _ y: Int, _ bound: Int) -> [Int] {
 3         var xs:[Int] = [1]
 4         var ys:[Int] = [1]
 5         
 6         if x > 1
 7         {
 8             var p:Int = x
 9             while(p <= bound)
10             {
11                 xs.append(p)
12                 p *= x
13             }
14         }
15         
16         if y > 1
17         {
18             var p:Int = y
19             while(p <= bound)
20             {
21                 ys.append(p)
22                 p *= y
23             }
24         }
25         
26         var s:Set<Int> = Set<Int>()
27         for xx in xs
28         {
29             for yy in ys
30             {
31                 if xx + yy <= bound
32                 {
33                     s.insert(xx + yy)
34                 }
35             }
36         }
37         return Array(s)
38     }
39 }

8ms

 1 class Solution {
 2     func powerfulIntegers(_ x: Int, _ y: Int, _ bound: Int) -> [Int] {
 3         var res = [Int]()
 4         var xx = [Int]()
 5         var yy = [Int]()
 6         if x == 0 {
 7             xx = [0]
 8         } else if x == 1 {
 9             xx = [1]
10         } else {
11             xx.append(1)
12             var tmp = x
13             while tmp <= bound {
14                 xx.append(tmp)
15                 tmp *= x
16             }
17         }
18         
19         if y == 0 {
20             yy = [0]
21         } else if y == 1 {
22             yy = [1]
23         } else {
24             yy.append(1)
25             var tmp = y
26             while tmp <= bound {
27                 yy.append(tmp)
28                 tmp *= y
29             }
30         }
31         
32         var tmp = 0
33         
34         for i in xx {
35             for u in yy {
36                 tmp = i + u
37                 if !res.contains(tmp) && tmp <= bound {
38                     res.append(tmp)
39                 }
40                 if tmp > bound {
41                     break
42                 }
43             }
44         }
45         
46         return res
47     }
48 }

16ms

 1 class Solution {
 2     func powerfulIntegers(_ x: Int, _ y: Int, _ bound: Int) -> [Int] {
 3         var result = [Int]()
 4         helper(x, y, 0, 0, bound, &result)
 5         return result
 6     }
 7     
 8     func helper(_ x: Int, _ y: Int, _ powerX: Int, _ powerY: Int, _ bound: Int, _ result :inout [Int]) {
 9         let sum = Int(pow(Double(x), Double(powerX)) + pow(Double(y), Double(powerY)))
10         if sum > bound {
11             return
12         }
13         
14         if !result.contains(sum) {
15             result.append(sum)
16         }
17         
18         if x > 1 {
19             helper(x, y, powerX + 1, powerY, bound, &result)
20         }
21         
22         if y > 1 {
23             helper(x, y, powerX, powerY + 1, bound, &result)
24         }
25     }
26 }

20ms

 1 class Solution {
 2     func powerfulIntegers(_ x: Int, _ y: Int, _ bound: Int) -> [Int] {
 3         if (x <= 0 && y <= 0) || (x == 1 && y == 0) || (x == 0 && y == 1) || (x == 1 && y == 1) {
 4             let a = Int(pow(Double(x), 0.0) + pow(Double(y), 0.0))
 5             let b = Int(pow(Double(x), 1.0) + pow(Double(y), 1.0))
 6             let c = Int(pow(Double(x), 1.0) + pow(Double(y), 0.0))
 7             let d = Int(pow(Double(x), 0.0) + pow(Double(y), 1.0))
 8             var s = Set<Int>()
 9             s.insert(a)
10             s.insert(b)
11             s.insert(c)
12             s.insert(d)
13             var arr = [Int]()
14             for n in s {
15                 if n <= bound {
16                     arr.append(n)
17                 }
18             }
19             return arr
20         }
21         var s = Set<Int>()
22         var p1 = 0
23         var sum = 0
24         var count = 0
25        out: while true {
26             var p2 = 0
27             while sum <= bound {
28                 sum = Int(pow(Double(x), Double(p1))) + Int(pow(Double(y), Double(p2)))
29                 p2 += 1
30                 if sum <= bound {
31                     s.insert(sum)
32                 }
33                 count += 1
34                 if count > 3000 {
35                     break out
36                 }
37             }
38             p1 += 1
39             sum = Int(pow(Double(x), Double(p1))) + Int(pow(Double(y), Double(0)))
40             if sum <= bound {
41                 
42             } else {
43                 break
44             }
45         }
46         return Array(s)
47     }
48 }
相關文章
相關標籤/搜索