Skip to content

Commit 0e245a0

Browse files
committed
add 1154
1 parent eb7fa21 commit 0e245a0

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "description"
3+
version = "0.1.0"
4+
authors = ["xargin <cao1988228@163.com>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
fn main() {
2+
println!("Hello, world!");
3+
}
4+
5+
/*
6+
* @lc app=leetcode.cn id=1154 lang=rust
7+
*
8+
* [1154] 一年中的第几天
9+
*/
10+
impl Solution {
11+
pub fn day_of_year(date: String) -> i32 {
12+
let day_for_normal = vec![31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
13+
let day_for_leap = vec![31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
14+
let ymd: Vec<&str> = date.split("-").collect();
15+
let (y, m, d) = (
16+
ymd[0].parse::<i32>().unwrap(),
17+
ymd[1].parse::<i32>().unwrap(),
18+
ymd[2].parse::<i32>().unwrap(),
19+
);
20+
let mut should_use = day_for_normal;
21+
if Self::is_leap(y) {
22+
should_use = day_for_leap;
23+
}
24+
let mut res = 0;
25+
(0..m - 1).for_each(|i| {
26+
res += should_use[i as usize];
27+
});
28+
res + d
29+
}
30+
fn is_leap(y: i32) -> bool {
31+
(y % 4 == 0 && y % 100 != 0) || y % 400 == 0
32+
}
33+
}
34+

0 commit comments

Comments
 (0)