Skip to content

Commit

Permalink
add: 一周中的第几天
Browse files Browse the repository at this point in the history
  • Loading branch information
yi-ge committed Dec 30, 2023
1 parent e494bc4 commit 12fc211
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -736,3 +736,7 @@ Rust标准库`std::collections`提供了4种通用容器类型,包含一下8
- [参加考试的最大学生数](src/math/maximum_students_taking_exam.rs) [位运算, 数组, 动态规划, 状态压缩, 矩阵]

- LeetCode 1349. 参加考试的最大学生数 <https://leetcode.cn/problems/maximum-students-taking-exam>

- [一周中的第几天](src/math/day_of_the_week.rs) [数学]

- LeetCode 1185. 一周中的第几天 <https://leetcode.cn/problems/day-of-the-week>
Binary file added images/math/day_of_the_week.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 29 additions & 0 deletions src/math/day_of_the_week.rs
Original file line number Diff line number Diff line change
@@ -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();
}
}
1 change: 1 addition & 0 deletions src/math/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
31 changes: 31 additions & 0 deletions tests/math/day_of_the_week_test.rs
Original file line number Diff line number Diff line change
@@ -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");
}
1 change: 1 addition & 0 deletions tests/math/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 12fc211

Please sign in to comment.