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

[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 @   为敢技术  阅读(403)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示
哥伦布
09:09发布
哥伦布
09:09发布
3°
多云
东南风
3级
空气质量
相对湿度
47%
今天
中雨
3°/15°
周三
中雨
3°/13°
周四
小雪
-1°/6°