提交 c0325eed 编写于 作者: 梦境迷离's avatar 梦境迷离

rust leetcode 204

上级 6283ca5b
......@@ -81,4 +81,5 @@ Leetcode Rust 实现
* [475 供暖器](src/leetcode_475.rs)
* [605 种花问题](src/leetcode_605.rs)
* [面试题10- I 斐波那契数列](src/interview_10_01.rs)
* [633 平方数之和](src/leetcode_633.rs)
\ No newline at end of file
* [633 平方数之和](src/leetcode_633.rs)
* [204 计数质数](src/leetcode_204.rs)
\ No newline at end of file
use std::collections::HashSet;
use crate::pre_structs::Solution;
///204. 计数质数
impl Solution {
//772MS,2.1MB
pub fn count_primes(n: i32) -> i32 {
fn is_primes(num: i32) -> bool {
let mut cnt = 0;
if num < 2 {
return false;
}
if num == 2 {
return true;
}
for i in 2..=f32::sqrt(num as f32) as i32 {
if num % i == 0 {
return false;
}
}
true
}
let mut cnt = 0;
for i in 0..n {
if is_primes(i) {
cnt += 1;
}
}
cnt
}
//排除法 埃拉托色尼筛选法
//12MS,3.4MB
pub fn count_primes2(n: i32) -> i32 {
if n < 2 {
return 0;
}
let mut bit_set = vec![true; n as usize];
let mut i = 2;
while i * i < n {
if bit_set[i as usize] {
let mut j = i * i;
while j < n {
bit_set[j as usize] = false;
j += i;
}
}
i += 1;
}
let cnt = bit_set
.iter()
.enumerate()
.filter(|(i, &x)| i >= &2 && i < &(n as usize) && x == true)
.count();
cnt as i32
}
}
#[cfg(test)]
mod test {
use crate::pre_structs::Solution;
#[test]
fn count_primes() {
let ret = Solution::count_primes(10);
let ret2 = Solution::count_primes2(10);
assert!(ret == ret2);
}
}
......@@ -41,6 +41,7 @@ pub mod leetcode_14;
pub mod leetcode_1403;
pub mod leetcode_1413;
pub mod leetcode_20;
pub mod leetcode_204;
pub mod leetcode_292;
pub mod leetcode_35;
pub mod leetcode_38;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册