add: 周赛385

完成1.2.3
This commit is contained in:
jackfiled 2024-02-18 14:25:02 +08:00
parent 0465d255a5
commit df537c895d
5 changed files with 181 additions and 0 deletions

View File

@ -1,3 +1,4 @@
#[macro_use] #[macro_use]
pub mod util; pub mod util;
pub mod problem; pub mod problem;
pub mod week_385;

3
src/week_385/mod.rs Normal file
View File

@ -0,0 +1,3 @@
mod problem1;
mod problem2;
mod problem3;

39
src/week_385/problem1.rs Normal file
View File

@ -0,0 +1,39 @@
pub struct Solution;
impl Solution {
pub fn count_prefix_suffix_pairs(words: Vec<String>) -> i32 {
let mut result = 0;
let words: Vec<Vec<char>> = words.iter()
.map(|s| {s.chars().collect()})
.collect();
for j in 1..words.len() {
for i in 0..j {
if Solution::is_prefix_and_suffix(&words[i], &words[j]) {
result += 1;
}
}
}
result
}
fn is_prefix_and_suffix(str1: &Vec<char>, str2: &Vec<char>) -> bool {
if str1.len() > str2.len() {
return false;
}
let mut prefix = 0;
let mut suffix = str2.len() - str1.len();
for c in str1 {
if *c != str2[prefix] || *c != str2[suffix] {
return false;
}
prefix += 1;
suffix += 1;
}
true
}
}

47
src/week_385/problem2.rs Normal file
View File

@ -0,0 +1,47 @@
pub struct Solution {}
use std::collections::HashSet;
impl Solution {
pub fn longest_common_prefix(arr1: Vec<i32>, arr2: Vec<i32>) -> i32 {
let mut result = 0;
let mut set = HashSet::new();
for num in arr1 {
let mut num = num;
while num != 0 {
set.insert(num);
num = num / 10;
}
}
for num in arr2 {
let mut len = 0;
let mut i = num;
while i != 0 {
len += 1;
i = i / 10;
}
let mut i = num;
while i != 0 {
if set.contains(&i) {
result = result.max(len);
}
len -= 1;
i = i / 10;
}
}
result
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_385_2() {
}
}

91
src/week_385/problem3.rs Normal file
View File

@ -0,0 +1,91 @@
pub struct Solution {}
use std::collections::HashMap;
impl Solution {
pub fn most_frequent_prime(mat: Vec<Vec<i32>>) -> i32 {
let direction = vec![
(0, 1),
(1, 0),
(-1, 0),
(0, -1),
(1, 1),
(-1, -1),
(1, -1),
(-1, 1),
];
let mut result = HashMap::new();
let n = mat.len() as i32;
let m = mat[0].len() as i32;
for i in 0..n {
for j in 0..m {
for d in &direction {
let mut num = 0;
let mut step = 0;
loop {
let x = (i + d.0 * step);
let y = (j + d.1 * step);
step += 1;
if x < 0 || x >= n || y < 0 || y >= m {
break;
}
num = num * 10 + mat[x as usize][y as usize];
if num > 10 {
if let Some(entry) = result.get_mut(&num) {
*entry += 1;
} else if Solution::is_prime(num) {
result.insert(num, 1);
}
}
}
}
}
}
let (mut best, mut num) = (-1, 0);
for (key, value) in result {
if num < value || (num == value && key > best) {
best = key;
num = value;
}
}
best
}
fn is_prime(num: i32) -> bool {
let mut i = 2;
while i * i <= num {
if num % i == 0 {
return false;
}
i += 1;
}
true
}
}
mod tests {
use super::*;
#[test]
fn test_385_3() {
assert_eq!(
19,
Solution::most_frequent_prime(vec![vec![1, 1], vec![9, 9], vec![1, 1]])
);
assert_eq!(
29,
Solution::most_frequent_prime(vec![vec![2], vec![9], vec![6]])
);
}
}