1. 程式人生 > >[Swift]LeetCode762. 二進制表示中質數個計算置位 | Prime Number of Set Bits in Binary Representation

[Swift]LeetCode762. 二進制表示中質數個計算置位 | Prime Number of Set Bits in Binary Representation

actor mes clas ted nump leetcode7 small 直接 fun

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 1s 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:

  1. L, R will be integers L <= R in the range [1, 10^6].
  2. R - L will be at most 10000.

給定兩個整數 LR ,找到閉區間 [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 不是質數)

註意:

  1. L, RL <= R 且在 [1, 10^6] 中的整數。
  2. R - L 的最大值為 10000。

Runtime: 16 ms Memory Usage: 18.6 MB
 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 }

[Swift]LeetCode762. 二進制表示中質數個計算置位 | Prime Number of Set Bits in Binary Representation