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

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-tqpimhap-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★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.git

(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.) github

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:app

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:ide

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

給定兩個整數 L 和 R ,找到閉區間 [L, R] 範圍內,計算置位位數爲質數的整數個數。spa

(注意,計算置位表明二進制表示中1的個數。例如 21 的二進制表示 10101 有 3 個計算置位。還有,1 不是質數。)code

示例 1:htm

輸入: L = 6, R = 10
輸出: 4
解釋:
6 -> 110 (2 個計算置位,2 是質數)
7 -> 111 (3 個計算置位,3 是質數)
9 -> 1001 (2 個計算置位,2 是質數)
10-> 1010 (2 個計算置位,2 是質數)

示例 2:blog

輸入: 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, R 是 L <= 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 }
相關文章
相關標籤/搜索