![[Swift Weekly Contest 118]LeetCode970. 强整数 | Powerful Integers,第1张 [Swift Weekly Contest 118]LeetCode970. 强整数 | Powerful Integers,第1张](/aiimages/%5BSwift+Weekly+Contest+118%5DLeetCode970.+%E5%BC%BA%E6%95%B4%E6%95%B0+%7C+Powerful+Integers.png)
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 = 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:
input: x = 3,y = 5,bound = 15 Output: [2,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,10]解释: 2 = 2^0 + 3^03 = 2^1 + 3^04 = 2^0 + 3^15 = 2^1 + 3^17 = 2^2 + 3^19 = 2^3 + 3^010 = 2^0 + 3^2
示例 2:
输入:x = 3,y = 5,bound = 15输出:[2,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 *= x13 }14 }15 16 if y > 117 {18 var p:Int = y19 while(p <= bound)20 {21 ys.append(p)22 p *= y23 }24 }25 26 var s:Set<Int> = Set<Int>()27 for xx in xs28 {29 for yy in ys30 {31 if xx + yy <= bound32 {33 s.insert(xx + yy)34 }35 }36 }37 return Array(s)38 }39 }总结
以上是内存溢出为你收集整理的[Swift Weekly Contest 118]LeetCode970. 强整数 | Powerful Integers全部内容,希望文章能够帮你解决[Swift Weekly Contest 118]LeetCode970. 强整数 | Powerful Integers所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)