Skip to content

Commit e90f17b

Browse files
committed
feat: add solutions to lc problem: No.1154
No.1154.Day of the Year
1 parent c7d02c3 commit e90f17b

File tree

7 files changed

+120
-119
lines changed

7 files changed

+120
-119
lines changed

solution/1100-1199/1154.Day of the Year/README.md

Lines changed: 48 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,19 @@
3838

3939
<!-- 这里可写通用的实现逻辑 -->
4040

41-
闰年 2 月有 29 天,平年 2 月有 28 天。
41+
**方法一:直接计算**
4242

43-
闰年的判断规则:`year % 100 == 0 || (year % 4 == 0 && year % 100 != 0)`
43+
根据题意,给定的日期是公元纪年法的日期,因此可以直接计算出该日期是当年的第几天。
44+
45+
首先,根据给定的日期计算出年月日,分别为 $y$, $m$, $d$。
46+
47+
然后,根据公元纪年法的闰年规则,计算出当年二月份的天数,闰年的二月份有 $29$ 天,平年的二月份有 $28$ 天。
48+
49+
> 闰年的计算规则是:年份能被 $400$ 整除,或者年份能被 $4$ 整除且不能被 $100$ 整除。
50+
51+
最后,根据给定的日期计算出当年的第几天,即把前面每个月的天数累加起来,再加上当月的天数即可。
52+
53+
时间复杂度为 $O(1)$,空间复杂度为 $O(1)$。
4454

4555
<!-- tabs:start -->
4656

@@ -51,10 +61,10 @@
5161
```python
5262
class Solution:
5363
def dayOfYear(self, date: str) -> int:
54-
year, month, day = (int(e) for e in date.split('-'))
55-
d = 29 if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0) else 28
56-
days = [31, d, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
57-
return sum(days[: month - 1]) + day
64+
y, m, d = (int(s) for s in date.split('-'))
65+
v = 29 if y % 400 == 0 or (y % 4 == 0 and y % 100) else 28
66+
days = [31, v, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
67+
return sum(days[:m-1]) + d
5868
```
5969

6070
### **Java**
@@ -64,13 +74,13 @@ class Solution:
6474
```java
6575
class Solution {
6676
public int dayOfYear(String date) {
67-
int year = Integer.parseInt(date.substring(0, 4));
68-
int month = Integer.parseInt(date.substring(5, 7));
69-
int day = Integer.parseInt(date.substring(8));
70-
int d = year % 400 == 0 || (year % 4 == 0 && year % 100 != 0) ? 29 : 28;
71-
int[] days = new int[] {31, d, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
72-
int ans = day;
73-
for (int i = 0; i < month - 1; ++i) {
77+
int y = Integer.parseInt(date.substring(0, 4));
78+
int m = Integer.parseInt(date.substring(5, 7));
79+
int d = Integer.parseInt(date.substring(8));
80+
int v = y % 400 == 0 || (y % 4 == 0 && y % 100 != 0) ? 29 : 28;
81+
int[] days = {31, v, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
82+
int ans = d;
83+
for (int i = 0; i < m - 1; ++i) {
7484
ans += days[i];
7585
}
7686
return ans;
@@ -84,13 +94,15 @@ class Solution {
8494
class Solution {
8595
public:
8696
int dayOfYear(string date) {
87-
int year = stoi(date.substr(0, 4));
88-
int month = stoi(date.substr(5, 7));
89-
int day = stoi(date.substr(8));
90-
int d = year % 400 == 0 || (year % 4 == 0 && year % 100 != 0) ? 29 : 28;
91-
int days[] = {31, d, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
92-
int ans = day;
93-
for (int i = 0; i < month - 1; ++i) ans += days[i];
97+
int y = stoi(date.substr(0, 4));
98+
int m = stoi(date.substr(5, 2));
99+
int d = stoi(date.substr(8));
100+
int v = y % 400 == 0 || (y % 4 == 0 && y % 100) ? 29 : 28;
101+
int days[] = {31, v, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
102+
int ans = d;
103+
for (int i = 0; i < m - 1; ++i) {
104+
ans += days[i];
105+
}
94106
return ans;
95107
}
96108
};
@@ -99,19 +111,19 @@ public:
99111
### **Go**
100112
101113
```go
102-
func dayOfYear(date string) int {
103-
year, _ := strconv.Atoi(date[:4])
104-
month, _ := strconv.Atoi(date[5:7])
105-
day, _ := strconv.Atoi(date[8:])
114+
func dayOfYear(date string) (ans int) {
115+
y, _ := strconv.Atoi(date[:4])
116+
m, _ := strconv.Atoi(date[5:7])
117+
d, _ := strconv.Atoi(date[8:])
106118
days := []int{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
107-
if year%400 == 0 || (year%4 == 0 && year%100 != 0) {
108-
days[1]++
119+
if y%400 == 0 || (y%4 == 0 && y%100 != 0) {
120+
days[1] = 29
109121
}
110-
ans := day
111-
for i := 0; i < month-1; i++ {
112-
ans += days[i]
122+
ans += d
123+
for _, v := range days[:m-1] {
124+
ans += v
113125
}
114-
return ans
126+
return
115127
}
116128
```
117129

@@ -123,17 +135,12 @@ func dayOfYear(date string) int {
123135
* @return {number}
124136
*/
125137
var dayOfYear = function (date) {
126-
const year = +date.slice(0, 4);
127-
const month = +date.slice(5, 7);
128-
const day = +date.slice(8);
129-
const d =
130-
year % 400 === 0 || (year % 4 === 0 && year % 100 !== 0) ? 29 : 28;
131-
const days = [31, d, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
132-
let ans = day;
133-
for (let i = 0; i < month - 1; ++i) {
134-
ans += days[i];
135-
}
136-
return ans;
138+
const y = +date.slice(0, 4);
139+
const m = +date.slice(5, 7);
140+
const d = +date.slice(8);
141+
const v = y % 400 == 0 || (y % 4 == 0 && y % 100) ? 29 : 28;
142+
const days = [31, v, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
143+
return days.slice(0, m - 1).reduce((a, b) => a + b, d);
137144
};
138145
```
139146

solution/1100-1199/1154.Day of the Year/README_EN.md

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,24 @@
4040
```python
4141
class Solution:
4242
def dayOfYear(self, date: str) -> int:
43-
year, month, day = (int(e) for e in date.split('-'))
44-
d = 29 if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0) else 28
45-
days = [31, d, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
46-
return sum(days[: month - 1]) + day
43+
y, m, d = (int(s) for s in date.split('-'))
44+
v = 29 if y % 400 == 0 or (y % 4 == 0 and y % 100) else 28
45+
days = [31, v, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
46+
return sum(days[:m-1]) + d
4747
```
4848

4949
### **Java**
5050

5151
```java
5252
class Solution {
5353
public int dayOfYear(String date) {
54-
int year = Integer.parseInt(date.substring(0, 4));
55-
int month = Integer.parseInt(date.substring(5, 7));
56-
int day = Integer.parseInt(date.substring(8));
57-
int d = year % 400 == 0 || (year % 4 == 0 && year % 100 != 0) ? 29 : 28;
58-
int[] days = new int[] {31, d, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
59-
int ans = day;
60-
for (int i = 0; i < month - 1; ++i) {
54+
int y = Integer.parseInt(date.substring(0, 4));
55+
int m = Integer.parseInt(date.substring(5, 7));
56+
int d = Integer.parseInt(date.substring(8));
57+
int v = y % 400 == 0 || (y % 4 == 0 && y % 100 != 0) ? 29 : 28;
58+
int[] days = {31, v, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
59+
int ans = d;
60+
for (int i = 0; i < m - 1; ++i) {
6161
ans += days[i];
6262
}
6363
return ans;
@@ -71,13 +71,15 @@ class Solution {
7171
class Solution {
7272
public:
7373
int dayOfYear(string date) {
74-
int year = stoi(date.substr(0, 4));
75-
int month = stoi(date.substr(5, 7));
76-
int day = stoi(date.substr(8));
77-
int d = year % 400 == 0 || (year % 4 == 0 && year % 100 != 0) ? 29 : 28;
78-
int days[] = {31, d, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
79-
int ans = day;
80-
for (int i = 0; i < month - 1; ++i) ans += days[i];
74+
int y = stoi(date.substr(0, 4));
75+
int m = stoi(date.substr(5, 2));
76+
int d = stoi(date.substr(8));
77+
int v = y % 400 == 0 || (y % 4 == 0 && y % 100) ? 29 : 28;
78+
int days[] = {31, v, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
79+
int ans = d;
80+
for (int i = 0; i < m - 1; ++i) {
81+
ans += days[i];
82+
}
8183
return ans;
8284
}
8385
};
@@ -86,19 +88,19 @@ public:
8688
### **Go**
8789
8890
```go
89-
func dayOfYear(date string) int {
90-
year, _ := strconv.Atoi(date[:4])
91-
month, _ := strconv.Atoi(date[5:7])
92-
day, _ := strconv.Atoi(date[8:])
91+
func dayOfYear(date string) (ans int) {
92+
y, _ := strconv.Atoi(date[:4])
93+
m, _ := strconv.Atoi(date[5:7])
94+
d, _ := strconv.Atoi(date[8:])
9395
days := []int{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
94-
if year%400 == 0 || (year%4 == 0 && year%100 != 0) {
95-
days[1]++
96+
if y%400 == 0 || (y%4 == 0 && y%100 != 0) {
97+
days[1] = 29
9698
}
97-
ans := day
98-
for i := 0; i < month-1; i++ {
99-
ans += days[i]
99+
ans += d
100+
for _, v := range days[:m-1] {
101+
ans += v
100102
}
101-
return ans
103+
return
102104
}
103105
```
104106

@@ -110,17 +112,12 @@ func dayOfYear(date string) int {
110112
* @return {number}
111113
*/
112114
var dayOfYear = function (date) {
113-
const year = +date.slice(0, 4);
114-
const month = +date.slice(5, 7);
115-
const day = +date.slice(8);
116-
const d =
117-
year % 400 === 0 || (year % 4 === 0 && year % 100 !== 0) ? 29 : 28;
118-
const days = [31, d, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
119-
let ans = day;
120-
for (let i = 0; i < month - 1; ++i) {
121-
ans += days[i];
122-
}
123-
return ans;
115+
const y = +date.slice(0, 4);
116+
const m = +date.slice(5, 7);
117+
const d = +date.slice(8);
118+
const v = y % 400 == 0 || (y % 4 == 0 && y % 100) ? 29 : 28;
119+
const days = [31, v, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
120+
return days.slice(0, m - 1).reduce((a, b) => a + b, d);
124121
};
125122
```
126123

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
class Solution {
22
public:
33
int dayOfYear(string date) {
4-
int year = stoi(date.substr(0, 4));
5-
int month = stoi(date.substr(5, 7));
6-
int day = stoi(date.substr(8));
7-
int d = year % 400 == 0 || (year % 4 == 0 && year % 100 != 0) ? 29 : 28;
8-
int days[] = {31, d, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
9-
int ans = day;
10-
for (int i = 0; i < month - 1; ++i) ans += days[i];
4+
int y = stoi(date.substr(0, 4));
5+
int m = stoi(date.substr(5, 2));
6+
int d = stoi(date.substr(8));
7+
int v = y % 400 == 0 || (y % 4 == 0 && y % 100) ? 29 : 28;
8+
int days[] = {31, v, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
9+
int ans = d;
10+
for (int i = 0; i < m - 1; ++i) {
11+
ans += days[i];
12+
}
1113
return ans;
1214
}
1315
};
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
func dayOfYear(date string) int {
2-
year, _ := strconv.Atoi(date[:4])
3-
month, _ := strconv.Atoi(date[5:7])
4-
day, _ := strconv.Atoi(date[8:])
1+
func dayOfYear(date string) (ans int) {
2+
y, _ := strconv.Atoi(date[:4])
3+
m, _ := strconv.Atoi(date[5:7])
4+
d, _ := strconv.Atoi(date[8:])
55
days := []int{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
6-
if year%400 == 0 || (year%4 == 0 && year%100 != 0) {
7-
days[1]++
6+
if y%400 == 0 || (y%4 == 0 && y%100 != 0) {
7+
days[1] = 29
88
}
9-
ans := day
10-
for i := 0; i < month-1; i++ {
11-
ans += days[i]
9+
ans += d
10+
for _, v := range days[:m-1] {
11+
ans += v
1212
}
13-
return ans
13+
return
1414
}

solution/1100-1199/1154.Day of the Year/Solution.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
class Solution {
22
public int dayOfYear(String date) {
3-
int year = Integer.parseInt(date.substring(0, 4));
4-
int month = Integer.parseInt(date.substring(5, 7));
5-
int day = Integer.parseInt(date.substring(8));
6-
int d = year % 400 == 0 || (year % 4 == 0 && year % 100 != 0) ? 29 : 28;
7-
int[] days = new int[] {31, d, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
8-
int ans = day;
9-
for (int i = 0; i < month - 1; ++i) {
3+
int y = Integer.parseInt(date.substring(0, 4));
4+
int m = Integer.parseInt(date.substring(5, 7));
5+
int d = Integer.parseInt(date.substring(8));
6+
int v = y % 400 == 0 || (y % 4 == 0 && y % 100 != 0) ? 29 : 28;
7+
int[] days = {31, v, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
8+
int ans = d;
9+
for (int i = 0; i < m - 1; ++i) {
1010
ans += days[i];
1111
}
1212
return ans;

solution/1100-1199/1154.Day of the Year/Solution.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,10 @@
33
* @return {number}
44
*/
55
var dayOfYear = function (date) {
6-
const year = +date.slice(0, 4);
7-
const month = +date.slice(5, 7);
8-
const day = +date.slice(8);
9-
const d =
10-
year % 400 === 0 || (year % 4 === 0 && year % 100 !== 0) ? 29 : 28;
11-
const days = [31, d, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
12-
let ans = day;
13-
for (let i = 0; i < month - 1; ++i) {
14-
ans += days[i];
15-
}
16-
return ans;
6+
const y = +date.slice(0, 4);
7+
const m = +date.slice(5, 7);
8+
const d = +date.slice(8);
9+
const v = y % 400 == 0 || (y % 4 == 0 && y % 100) ? 29 : 28;
10+
const days = [31, v, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
11+
return days.slice(0, m - 1).reduce((a, b) => a + b, d);
1712
};
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class Solution:
22
def dayOfYear(self, date: str) -> int:
3-
year, month, day = (int(e) for e in date.split('-'))
4-
d = 29 if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0) else 28
5-
days = [31, d, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
6-
return sum(days[: month - 1]) + day
3+
y, m, d = (int(s) for s in date.split('-'))
4+
v = 29 if y % 400 == 0 or (y % 4 == 0 and y % 100) else 28
5+
days = [31, v, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
6+
return sum(days[:m-1]) + d

0 commit comments

Comments
 (0)