在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ Write a program to find the Super ugly numbers are positive numbers whose all prime factors are in the given prime list Example: Input: n = 12, Note:
编写一段程序来查找第 超级丑数是指其所有质因数都是长度为 示例: 输入: n = 12, 说明:
116 ms 1 class Solution { 2 func nthSuperUglyNumber(_ n: Int, _ primes: [Int]) -> Int { 3 let count = primes.count 4 5 var index = Array(repeatElement(0, count: count)) 6 var value = primes 7 var temp = 0 8 9 var ugly = [1] 10 for _ in 0..<n - 1 { 11 temp = Int.max 12 for j in 0..<count { 13 temp = min(temp, value[j]) 14 } 15 ugly.append(temp) 16 for j in 0..<count { 17 if temp == value[j] { 18 index[j] += 1 19 value[j] = ugly[index[j]] * primes[j] 20 } 21 } 22 } 23 return ugly[n - 1] 24 } 25 } 556ms 1 class Solution { 2 func nthSuperUglyNumber(_ n: Int, _ primes: [Int]) -> Int { 3 var res = [1] 4 var c = Array(repeating: 0, count: primes.count) 5 6 for _ in 0 ..< n-1 { 7 var comp = [Int]() 8 for i in 0 ..< primes.count { 9 comp.append(res[c[i]] * primes[i]) 10 } 11 12 let minP = comp.min()! 13 res.append(minP) 14 15 for j in 0 ..< comp.count where comp[j] == minP { 16 c[j] += 1 17 } 18 } 19 20 return res.last! 21 } 22 }
|
请发表评论