Skip to content

Commit

Permalink
50
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzhj514 committed Feb 19, 2025
1 parent 71c3929 commit deb34c0
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 26 deletions.
12 changes: 7 additions & 5 deletions exercises/easy/algorithm17.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ use std::collections::HashSet;
pub fn intersection(nums1: Vec<i32>, nums2: Vec<i32>) -> Vec<i32> {
// TODO: Implement the logic to find the intersection of two arrays
// Placeholder return value
let set1:HashSet<_> = nums1.into_iter().collect();
let set2:HashSet<_> = nums2.into_iter().collect();
let union_set: HashSet<_> = set1.intersection(&set2).cloned().collect();
let union_vec: Vec<_> = union_set.into_iter().collect();
union_vec
let mut result = Vec::new(); // Placeholder return value
for i in 0..nums1.len() {
if nums2.contains(&nums1[i]) && !result.contains(&nums1[i]) {
result.push(nums1[i]);
}
}
return result
}

#[cfg(test)]
Expand Down
9 changes: 8 additions & 1 deletion exercises/normal/solution1/src/count_distinct.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
pub fn new_count_distinct(input_str: &str) -> usize {
todo!()
let parts: Vec<&str> = input_str.split(',').collect();
let mut new = Vec::new();
for part in parts{
if !new.contains(&part){
new.push(part);
}
}
new.len()
}
55 changes: 54 additions & 1 deletion exercises/normal/solution2/src/converter.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,57 @@

pub fn convert_base(num_str: &str, to_base: u32) -> String {
// TODO: 这里写逻辑
todo!()
let ori_str = num_str.clone();
let parts: Vec<&str> = num_str.split(|c| c == '(' || c == ')').collect();

let mut first_num: i32 = parts[0].trim().parse().unwrap();
let second_num: i32 = parts[1].trim().parse().unwrap();
if second_num != 10{
first_num = n_to_decimal(second_num as u32, first_num as u32)
}
dec_to_n(to_base, first_num as u32)
}

fn n_to_decimal(n: u32, num: u32) -> i32 {
// 将 i32 转换为字符串
let num_str = num.to_string();

// 从字符串中提取每一位数字,并计算十进制值
let mut decimal = 0;
let len = num_str.len();
for (i, c) in num_str.chars().enumerate() {
// 将字符转换为数字
let digit = c.to_digit(10).unwrap() as i32;
// 计算当前位的权重
let power = len - i - 1;
// 累加到十进制结果中
decimal += digit * n.pow(power as u32) as i32;
}
decimal
}
fn dec_to_n(n: u32, num: u32) -> String {
if n < 2 || n > 36 {
panic!("Base must be between 2 and 36");
}

// 定义字符集,用于表示 0-9 和 A-Z
let digits = "0123456789abcdefghijklmnopqrstuvwxyz";

// 特殊情况:0 的任何进制表示都是 "0"
if num == 0 {
return "0".to_string();
}

let mut num = num;
let mut result = String::new();

// 循环除法,直到 num 为 0
while num > 0 {
let remainder = num % n;
num /= n;
// 将余数对应的字符添加到结果字符串的前面
result.insert_str(0, &digits[remainder as usize..remainder as usize + 1]);
}

result
}
8 changes: 6 additions & 2 deletions exercises/normal/solution3/src/calc_logic.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
pub fn new_birthday_probability(n: u32) -> f64 {
// TODO: 这里写逻辑
todo!()
}
let mut num:f64 = 1.0;
for i in 1..=n{
num *= (365.0 + i as f64 - n as f64) / (365.0);
}
1.0-num
}
14 changes: 12 additions & 2 deletions exercises/normal/solution4/src/rec_mc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
pub fn dp_rec_mc(amount: u32) -> u32 {
pub fn dp_rec_mc(mut amount: u32) -> u32 {
// TODO: 这里写逻辑
todo!()
const CASHES: [u32; 8] = [1, 2, 5, 10, 20, 30, 50, 100];
let mut a = 0;
let mut b = 0;
let mut res = 0;
for i in 0..8{
a = amount%CASHES[7-i];
b = (amount - a)/CASHES[7-i];
res += b;
amount = a;
}
res
}
20 changes: 19 additions & 1 deletion exercises/normal/solution5/src/fibnacci.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
pub fn odd_fibnacci_sum(threshold: u32) -> u32 {
// TODO: 这里写逻辑
todo!()
let mut index = 0;
let mut res = 0;
while fib(index) < threshold{
if fib(index) % 2 == 1{
res += fib(index);
}
index += 1;
}
res
}
pub fn fib(n:u32)->u32
{
if n == 0 {
return 0;
}else if n == 1 {
return 1;
}else {
return fib(n-2) + fib(n-1);
}
}
28 changes: 14 additions & 14 deletions report.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,28 +102,28 @@
},
{
"name": "solution1",
"result": false,
"score": 0
"result": true,
"score": 6
},
{
"name": "solution2",
"result": false,
"score": 0
"result": true,
"score": 6
},
{
"name": "solution3",
"result": false,
"score": 0
"result": true,
"score": 6
},
{
"name": "solution4",
"result": false,
"score": 0
"result": true,
"score": 6
},
{
"name": "solution5",
"result": false,
"score": 0
"result": true,
"score": 6
},
{
"name": "solutiont1",
Expand Down Expand Up @@ -153,9 +153,9 @@
],
"statistics": {
"total_exercises": 30,
"total_successes": 20,
"total_failures": 10,
"total_score": 20,
"total_time": 43
"total_successes": 25,
"total_failures": 5,
"total_score": 50,
"total_time": 37
}
}

0 comments on commit deb34c0

Please sign in to comment.