• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode970.强整数|PowerfulIntegers

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10228288.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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.

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

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

Example 1:

Input: x = 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:

Input: x = 15
Output: [2,4,6,8,10,14]

Note:

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

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

 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 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
OpenStack Swift All In One安装部署流程与简单使用发布时间:2022-07-13
下一篇:
仅用递归函数操作逆序一个栈(Swift4)发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap