为有牺牲多壮志,敢教日月换新天。

[Swift]LeetCode204. 计数质数 | Count Primes

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9745292.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

Count the number of prime numbers less than a non-negative number, n.

Example:

Input: 10
Output: 4
Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7.

统计所有小于非负整数 的质数的数量。

示例:

输入: 10
输出: 4
解释: 小于 10 的质数一共有 4 个, 它们是 2, 3, 5, 7 。

88ms
 1 class Solution {
 2     func countPrimes(_ n: Int) -> Int {
 3         guard n > 2 else { return 0 }
 4         var sieve = [Bool](repeating: true, count: n)
 5         var count = n / 2
 6         var i = 3
 7         
 8         while i * i < n {
 9             if sieve[i] {
10                 var j = i * i
11 
12                 while j < n {
13                     if sieve[j] {
14                         count -= 1 
15                         sieve[j] = false
16                     }
17                     j += 2 * i 
18                 }
19             }
20             i += 2
21         }
22         
23         return count 
24     }
25 }

356ms

 1 class Solution{
 2     func countPrimes(_ n: Int) -> Int {
 3         if n < 3 { return 0 }
 4         var isPrime = Array.init(repeating: true, count: n)
 5         var i = 2;
 6         while i*i < n {
 7             if(!isPrime[i]) {
 8                 i += 1;
 9                 continue
10             }
11             var j = i*i
12             while (j < n) {
13                 isPrime[j] = false
14                 j += i;
15             }
16             i += 1;
17         }
18         var counter = 0
19         for k in 2..<n {
20             if isPrime[k] {
21                 counter += 1
22             }
23         }
24         return counter
25     }
26 }

380ms

 1 class Solution {
 2     func countPrimes(_ n: Int) -> Int {
 3         if n <= 0 {
 4             return 0
 5         }
 6         let newN = n - 1
 7         let sqrtN = Int(sqrt(Double(newN)))
 8         if n < 4 {
 9             return newN == 1 ? 0 : sqrtN
10         }
11         var nums = [Bool](repeating: true, count: n)
12 
13         var count = newN - 1
14         for num in 0..<newN {
15             nums[num] = true
16         }
17         nums[0] = false
18         nums[1] = true
19         
20         for index in 2..<(sqrtN + 1){
21             var j = index * index
22             if nums[j - 1] {
23                 while j < n {
24                     if nums[j - 1] {
25                         count = count - 1
26                         nums[j - 1] = false
27                     }
28                     j = j + index
29                 }
30             }
31         }
32         
33         return count
34     }
35 }

408ms

 1 class Solution {
 2     func countPrimes(_ n: Int) -> Int {        
 3         if n < 3 {
 4             return 0
 5         }
 6         if n == 3 {
 7             return 1
 8         }
 9         var array = [Bool](repeating: true, count: n)
10         for i in 2...Int(sqrt(Double(n))) {
11             if i * i >= n { break }
12             if !array[i] { continue }
13             var j = i * i
14             while j < n {
15                 array[j] = false
16                 j = j + i
17             }
18         }
19         var count = 0
20         for i in 2..<n {
21             if array[i] {
22                 count += 1
23             }
24         }
25         return count
26     }
27 }

416ms

 1 class Solution 
 2 {
 3     
 4     func countPrimes( _ n: Int ) -> Int 
 5     {
 6         var isPrime: [ Bool ] = Array< Bool >.init( repeating: true, count: max( 2, n+1 ) )
 7         
 8         isPrime[ 0 ] = false
 9         isPrime[ 1 ] = false
10         var primeCount: Int = 0
11 
12         for primeVal in ( 0 ... n )
13         {
14             if isPrime[ primeVal ]
15             {
16                 if primeVal < n
17                 { 
18                     primeCount += 1
19                 }
20                 var factor: Int = 2 * primeVal
21                 while factor <= n
22                 {
23                     isPrime[ factor ] = false
24                     factor += primeVal
25                 }
26             }
27         }
28         return primeCount
29     }
30 }

 

posted @ 2018-10-05 17:16  为敢技术  阅读(394)  评论(0编辑  收藏  举报