[Swift Weekly Contest 118]LeetCode970. 强整数 | Powerful Int
Given two non-negative integers? Return a list of all?powerful?integers that have value less than or equal to? 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 = 2,y = 3,bound = 10 输出:[2,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,14] 提示:
?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 } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |