diff --git a/README.md b/README.md index e4ad94b..84f7b66 100644 --- a/README.md +++ b/README.md @@ -736,3 +736,7 @@ Rust标准库`std::collections`提供了4种通用容器类型,包含一下8 - [参加考试的最大学生数](src/math/maximum_students_taking_exam.rs) [位运算, 数组, 动态规划, 状态压缩, 矩阵] - LeetCode 1349. 参加考试的最大学生数 + +- [一周中的第几天](src/math/day_of_the_week.rs) [数学] + + - LeetCode 1185. 一周中的第几天 diff --git a/images/math/day_of_the_week.jpeg b/images/math/day_of_the_week.jpeg new file mode 100644 index 0000000..b5afa05 Binary files /dev/null and b/images/math/day_of_the_week.jpeg differ diff --git a/src/math/day_of_the_week.rs b/src/math/day_of_the_week.rs new file mode 100644 index 0000000..768d4df --- /dev/null +++ b/src/math/day_of_the_week.rs @@ -0,0 +1,29 @@ +// 一周中的第几天 +// https://leetcode.cn/problems/day-of-the-week +// INLINE ../../images/math/day_of_the_week.jpeg + +pub struct Solution; + +impl Solution { + pub fn day_of_the_week(day: i32, month: i32, year: i32) -> String { + let week = vec![ + "Monday", + "Tuesday", + "Wednesday", + "Thursday", + "Friday", + "Saturday", + "Sunday", + ]; + let month_days = vec![31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30]; + let mut days = 365 * (year - 1971) + (year - 1969) / 4; + for i in 0..(month - 1) { + days += month_days[i as usize]; + } + if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) && month > 2 { + days += 1; + } + days += day; + return week[(days + 3) as usize % 7].to_string(); + } +} diff --git a/src/math/mod.rs b/src/math/mod.rs index 54022e9..0aff0db 100644 --- a/src/math/mod.rs +++ b/src/math/mod.rs @@ -18,3 +18,4 @@ pub mod find_the_pivot_integer; pub mod next_greater_numerically_balanced_number; pub mod number_of_burgers_with_no_waste_of_ingredients; pub mod maximum_students_taking_exam; +pub mod day_of_the_week; diff --git a/tests/math/day_of_the_week_test.rs b/tests/math/day_of_the_week_test.rs new file mode 100644 index 0000000..5be2cdd --- /dev/null +++ b/tests/math/day_of_the_week_test.rs @@ -0,0 +1,31 @@ +use rust_practice::math::day_of_the_week::Solution; + +#[test] +fn day_of_the_week() { + // 示例 1: + // 输入:day = 31, month = 8, year = 2019 + // 输出:"Saturday" + let day = 31; + let month = 8; + let year = 2019; + let result = Solution::day_of_the_week(day, month, year); + assert_eq!(result, "Saturday"); + + // 示例 2: + // 输入:day = 18, month = 7, year = 1999 + // 输出:"Sunday" + let day = 18; + let month = 7; + let year = 1999; + let result = Solution::day_of_the_week(day, month, year); + assert_eq!(result, "Sunday"); + + // 示例 3: + // 输入:day = 15, month = 8, year = 1993 + // 输出:"Sunday" + let day = 15; + let month = 8; + let year = 1993; + let result = Solution::day_of_the_week(day, month, year); + assert_eq!(result, "Sunday"); +} diff --git a/tests/math/mod.rs b/tests/math/mod.rs index 54a37d8..99fdede 100644 --- a/tests/math/mod.rs +++ b/tests/math/mod.rs @@ -18,3 +18,4 @@ pub mod find_the_pivot_integer_test; pub mod next_greater_numerically_balanced_number_test; pub mod number_of_burgers_with_no_waste_of_ingredients_test; pub mod maximum_students_taking_exam_test; +pub mod day_of_the_week_test;