[Swift]LeetCode762. 二进制表示中质数个计算置位 | Prime Number of Set Bits in Binary Representation
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10533081.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
Given two integers L
and R
, find the count of numbers in the range [L, R]
(inclusive) having a prime number of set bits in their binary representation.
(Recall that the number of set bits an integer has is the number of 1
s present when written in binary. For example, 21
written in binary is 10101
which has 3 set bits. Also, 1 is not a prime.)
Example 1:
Input: L = 6, R = 10 Output: 4 Explanation: 6 -> 110 (2 set bits, 2 is prime) 7 -> 111 (3 set bits, 3 is prime) 9 -> 1001 (2 set bits , 2 is prime) 10->1010 (2 set bits , 2 is prime)
Example 2:
Input: L = 10, R = 15 Output: 5 Explanation: 10 -> 1010 (2 set bits, 2 is prime) 11 -> 1011 (3 set bits, 3 is prime) 12 -> 1100 (2 set bits, 2 is prime) 13 -> 1101 (3 set bits, 3 is prime) 14 -> 1110 (3 set bits, 3 is prime) 15 -> 1111 (4 set bits, 4 is not prime)
Note:
L, R
will be integersL <= R
in the range[1, 10^6]
.R - L
will be at most 10000.
给定两个整数 L
和 R
,找到闭区间 [L, R]
范围内,计算置位位数为质数的整数个数。
(注意,计算置位代表二进制表示中1的个数。例如 21
的二进制表示 10101
有 3 个计算置位。还有,1 不是质数。)
示例 1:
输入: L = 6, R = 10 输出: 4 解释: 6 -> 110 (2 个计算置位,2 是质数) 7 -> 111 (3 个计算置位,3 是质数) 9 -> 1001 (2 个计算置位,2 是质数) 10-> 1010 (2 个计算置位,2 是质数)
示例 2:
输入: L = 10, R = 15 输出: 5 解释: 10 -> 1010 (2 个计算置位, 2 是质数) 11 -> 1011 (3 个计算置位, 3 是质数) 12 -> 1100 (2 个计算置位, 2 是质数) 13 -> 1101 (3 个计算置位, 3 是质数) 14 -> 1110 (3 个计算置位, 3 是质数) 15 -> 1111 (4 个计算置位, 4 不是质数)
注意:
L, R
是L <= R
且在[1, 10^6]
中的整数。R - L
的最大值为 10000。
1 class Solution { 2 func countPrimeSetBits(_ L: Int, _ R: Int) -> Int { 3 4 var count = 0 5 for i in L...R { 6 let nonzeroBitCount = i.nonzeroBitCount 7 8 if isPrime(nonzeroBitCount) { 9 count += 1 10 } 11 } 12 13 return count 14 } 15 16 private func isPrime(_ num: Int) -> Bool { 17 guard num > 1 else { 18 return false 19 } 20 21 guard num != 2 else { 22 return true 23 } 24 25 guard num & 1 == 1 else { 26 return false 27 } 28 29 var i = 3 30 31 while i * i <= num, num % i != 0 { 32 i += 2 33 } 34 35 return i * i > num 36 } 37 }
36ms
1 class Solution { 2 func countPrimeSetBits(_ L: Int, _ R: Int) -> Int { 3 4 var count = 0 5 6 for i in L...R { 7 8 let bitCode = i.nonzeroBitCount 9 if isSmallPrime(bitCode) { 10 count += 1 11 } 12 } 13 14 return count 15 } 16 17 func isSmallPrime(_ i: Int) -> Bool { 18 19 switch i { 20 case 2,3,5,7,11,13,17,19: 21 return true 22 default: 23 return false 24 } 25 } 26 }
52ms
1 class Solution { 2 func countPrimeSetBits(_ L: Int, _ R: Int) -> Int { 3 var count = 0 4 for num in L...R { 5 6 let bits = getSetBits(num) 7 8 if isPrime(bits) { 9 count += 1 10 } 11 12 } 13 14 return count 15 } 16 17 18 func getSetBits(_ num : Int) -> Int { 19 var count = 0 20 var v = num 21 22 while v >= 1 { 23 v &= v - 1 24 count += 1 25 } 26 return count 27 } 28 29 func isPrime(_ n : Int ) -> Bool { 30 if n <= 1 { return false } 31 if n <= 3 { return true } 32 33 if n % 2 == 0 || n % 3 == 0 { return false } 34 35 var i = 5 36 while Int(pow(Double(i), Double(i))) <= n { 37 if n % i == 0 || n%(i+2) == 0 { 38 return false 39 } 40 41 i = i + 6 42 } 43 44 return true 45 } 46 }
96ms
1 class Solution { 2 func countPrimeSetBits(_ L: Int, _ R: Int) -> Int { 3 var res = 0 4 let primes = Set([2, 3, 5, 7, 11, 13, 17, 19]) 5 6 for i in L...R { 7 res += primes.contains(bitCount(i)) ? 1 : 0 8 } 9 10 return res 11 } 12 13 private func bitCount(_ n: Int) -> Int { 14 var count = 0 15 var n = n 16 17 while n > 0 { 18 n &= n - 1 19 count += 1 20 } 21 22 return count 23 } 24 }
372ms
1 class Solution { 2 func countPrimeSetBits(_ L: Int, _ R: Int) -> Int { 3 4 var count = 0 5 for i in L...R { 6 let nonzeroBitCount = i.nonzeroBitCount 7 8 var isPrime = true 9 if nonzeroBitCount == 1 { 10 isPrime = false 11 } else { 12 for factor in stride(from: 2, to: nonzeroBitCount, by: 1) { 13 if nonzeroBitCount % factor == 0 { 14 isPrime = false 15 break 16 } 17 } 18 } 19 20 count += isPrime ? 1 : 0 21 } 22 23 return count 24 } 25 }
460ms
1 class Solution { 2 func countPrimeSetBits(_ L: Int, _ R: Int) -> Int { 3 let primeMap = [0,0,1,1,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,1,0,1,0] 4 5 var count = 0 6 7 for num in L...R { 8 var n = num 9 var oneCount = 0 10 while n > 0 { 11 if n & 1 == 1 { 12 oneCount += 1 13 } 14 n >>= 1 15 } 16 count += primeMap[oneCount] 17 } 18 19 return count 20 } 21 }
620ms
1 class Solution { 2 // 输入区间为 1...10^6, 既最大为20位, 直接取20以内质数 3 let c = [2, 3, 5, 7, 11, 13, 17, 19] 4 5 func countPrimeSetBits(_ L: Int, _ R: Int) -> Int { 6 return (L...R).reduce(0) { 7 $0 + (c.contains($1.nonzeroBitCount) ? 1 : 0) 8 } 9 } 10 }
944ms
1 class Solution { 2 3 func countPrimeSetBits(_ L: Int, _ R: Int) -> Int { 4 var numPrime = 0 5 let primes = [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31] 6 for i in L...R { 7 var numOnes = 0 8 for j in 0..<32 { 9 if (i >> j) & 1 != 0 { 10 numOnes += 1 11 } 12 } 13 if primes.contains(numOnes) { 14 numPrime += 1 15 } 16 } 17 return numPrime 18 } 19 20 }
1460ms
1 class Solution { 2 func countPrimeSetBits(_ L: Int, _ R: Int) -> Int { 3 var result: Int = 0 4 for i in L...R { 5 let binaryString = String(i, radix: 2) 6 if isPrime(numberOfSetBits(binaryString)) { result += 1 } 7 } 8 9 return result 10 } 11 12 func isPrime(_ num: Int) -> Bool { 13 guard num >= 2 else { return false } 14 15 for i in 2..<num { 16 if num % i == 0 { return false } 17 } 18 19 return true 20 } 21 22 func numberOfSetBits(_ text: String) -> Int { 23 guard text != "" else { return 0 } 24 var result: Int = 0 25 26 text.characters.forEach { (char) in 27 if char == "1" { result += 1 } 28 } 29 30 return result 31 } 32 }
1840ms
1 class Solution { 2 func countPrimeSetBits(_ L: Int, _ R: Int) -> Int { 3 var statistics: [Int] = Array(repeating: 0, count: R-L+1) 4 for (i, v) in (L...R).enumerated() { 5 // 二进制操作数 6 var vt = v 7 // 二进制置位累加值 8 var va = 0 9 while vt > 0 { 10 if (vt >> 1) * 2 != vt { 11 va += 1 12 } 13 vt = vt >> 1 14 } 15 statistics[i] = va 16 } 17 return filterPrimeNumber(for: statistics) 18 } 19 // 输入区间为 1...10^6, 既最大为20位, 直接取20以内质数 20 func filterPrimeNumber(for array: [Int]) -> Int { 21 let c = [2, 3, 5, 7, 11, 13, 17, 19] 22 var accumulator = 0 23 array.forEach({ 24 if c.contains($0) { accumulator += 1 } 25 }) 26 return accumulator 27 } 28 }
1948ms
1 class Solution { 2 func countPrimeSetBits(_ L: Int, _ R: Int) -> Int { 3 var count = 0 4 var out: [Int] = [] 5 for i in L...R { 6 let bin = String(i, radix: 2) 7 var numberOfOnes = 0 8 for char in bin { 9 if char == "1" { 10 numberOfOnes += 1 11 } 12 } 13 if isPrime(number: numberOfOnes) { 14 out.append(i) 15 } 16 } 17 return out.count 18 } 19 20 func isPrime(number: Int) -> Bool { 21 return number > 1 && !(2..<number).contains { number % $0 == 0 } 22 } 23 }
2648ms
1 class Solution { 2 func countPrimeSetBits(_ L: Int, _ R: Int) -> Int { 3 var result = 0 4 for v in L...R { 5 if isPrime(Array(String(v, radix: 2)).filter {$0 == "1"}.count) { 6 result += 1 7 } 8 } 9 10 return result 11 } 12 13 func isPrime(_ number: Int) -> Bool { 14 return number > 1 && !(2..<Int(sqrt(Double(number)))+1).contains { number % $0 == 0 } 15 } 16 }