Skip to content

Commit 5e04121

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

File tree

7 files changed

+221
-38
lines changed

7 files changed

+221
-38
lines changed

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

+81-2
Original file line numberDiff line numberDiff line change
@@ -45,27 +45,106 @@
4545
<li><code>date</code> 表示的范围从 1900 年 1 月 1 日至 2019 年 12 月 31 日。</li>
4646
</ul>
4747

48-
4948
## 解法
5049

5150
<!-- 这里可写通用的实现逻辑 -->
5251

52+
闰年 2 月有 29 天,平年 2 月有 28 天。
53+
54+
闰年的判断规则:`year % 100 == 0 || (year % 4 == 0 && year % 100 != 0)`
55+
5356
<!-- tabs:start -->
5457

5558
### **Python3**
5659

5760
<!-- 这里可写当前语言的特殊实现逻辑 -->
5861

5962
```python
60-
63+
class Solution:
64+
def dayOfYear(self, date: str) -> int:
65+
year, month, day = (int(e) for e in date.split('-'))
66+
d = 29 if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0) else 28
67+
days = [31, d, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
68+
return sum(days[: month - 1]) + day
6169
```
6270

6371
### **Java**
6472

6573
<!-- 这里可写当前语言的特殊实现逻辑 -->
6674

6775
```java
76+
class Solution {
77+
public int dayOfYear(String date) {
78+
int year = Integer.parseInt(date.substring(0, 4));
79+
int month = Integer.parseInt(date.substring(5, 7));
80+
int day = Integer.parseInt(date.substring(8));
81+
int d = year % 400 == 0 || (year % 4 == 0 && year % 100 != 0) ? 29 : 28;
82+
int[] days = new int[]{31, d, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
83+
int ans = day;
84+
for (int i = 0; i < month - 1; ++i) {
85+
ans += days[i];
86+
}
87+
return ans;
88+
}
89+
}
90+
```
91+
92+
### **C++**
93+
94+
```cpp
95+
class Solution {
96+
public:
97+
int dayOfYear(string date) {
98+
int year = stoi(date.substr(0, 4));
99+
int month = stoi(date.substr(5, 7));
100+
int day = stoi(date.substr(8));
101+
int d = year % 400 == 0 || (year % 4 == 0 && year % 100 != 0) ? 29 : 28;
102+
int days[] = {31, d, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
103+
int ans = day;
104+
for (int i = 0; i < month - 1; ++i) ans += days[i];
105+
return ans;
106+
}
107+
};
108+
```
109+
110+
### **Go**
111+
112+
```go
113+
func dayOfYear(date string) int {
114+
year, _ := strconv.Atoi(date[:4])
115+
month, _ := strconv.Atoi(date[5:7])
116+
day, _ := strconv.Atoi(date[8:])
117+
days := []int{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
118+
if year%400 == 0 || (year%4 == 0 && year%100 != 0) {
119+
days[1]++
120+
}
121+
ans := day
122+
for i := 0; i < month-1; i++ {
123+
ans += days[i]
124+
}
125+
return ans
126+
}
127+
```
68128

129+
### **JavaScript**
130+
131+
```js
132+
/**
133+
* @param {string} date
134+
* @return {number}
135+
*/
136+
var dayOfYear = function(date) {
137+
const year = +date.slice(0, 4);
138+
const month = +date.slice(5, 7);
139+
const day = +date.slice(8);
140+
const d = year % 400 === 0 || (year % 4 === 0 && year % 100 !== 0) ? 29 : 28;
141+
const days = [31, d, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
142+
let ans = day;
143+
for (let i = 0; i < month - 1; ++i) {
144+
ans += days[i];
145+
}
146+
return ans;
147+
};
69148
```
70149

71150
### **...**

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

+77-21
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,10 @@
66

77
<p>Given a string <code>date</code> representing a <a href="https://en.wikipedia.org/wiki/Gregorian_calendar" target="_blank">Gregorian&nbsp;calendar</a> date formatted as <code>YYYY-MM-DD</code>, return the day number of the year.</p>
88

9-
10-
119
<p>&nbsp;</p>
1210

1311
<p><strong>Example 1:</strong></p>
1412

15-
16-
1713
<pre>
1814

1915
<strong>Input:</strong> date = &quot;2019-01-09&quot;
@@ -24,12 +20,8 @@
2420

2521
</pre>
2622

27-
28-
2923
<p><strong>Example 2:</strong></p>
3024

31-
32-
3325
<pre>
3426

3527
<strong>Input:</strong> date = &quot;2019-02-10&quot;
@@ -38,12 +30,8 @@
3830

3931
</pre>
4032

41-
42-
4333
<p><strong>Example 3:</strong></p>
4434

45-
46-
4735
<pre>
4836

4937
<strong>Input:</strong> date = &quot;2003-03-01&quot;
@@ -52,12 +40,8 @@
5240

5341
</pre>
5442

55-
56-
5743
<p><strong>Example 4:</strong></p>
5844

59-
60-
6145
<pre>
6246

6347
<strong>Input:</strong> date = &quot;2004-03-01&quot;
@@ -66,14 +50,10 @@
6650

6751
</pre>
6852

69-
70-
7153
<p>&nbsp;</p>
7254

7355
<p><strong>Constraints:</strong></p>
7456

75-
76-
7757
<ul>
7858
<li><code>date.length == 10</code></li>
7959
<li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits</li>
@@ -87,13 +67,89 @@
8767
### **Python3**
8868

8969
```python
90-
70+
class Solution:
71+
def dayOfYear(self, date: str) -> int:
72+
year, month, day = (int(e) for e in date.split('-'))
73+
d = 29 if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0) else 28
74+
days = [31, d, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
75+
return sum(days[: month - 1]) + day
9176
```
9277

9378
### **Java**
9479

9580
```java
81+
class Solution {
82+
public int dayOfYear(String date) {
83+
int year = Integer.parseInt(date.substring(0, 4));
84+
int month = Integer.parseInt(date.substring(5, 7));
85+
int day = Integer.parseInt(date.substring(8));
86+
int d = year % 400 == 0 || (year % 4 == 0 && year % 100 != 0) ? 29 : 28;
87+
int[] days = new int[]{31, d, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
88+
int ans = day;
89+
for (int i = 0; i < month - 1; ++i) {
90+
ans += days[i];
91+
}
92+
return ans;
93+
}
94+
}
95+
```
96+
97+
### **C++**
98+
99+
```cpp
100+
class Solution {
101+
public:
102+
int dayOfYear(string date) {
103+
int year = stoi(date.substr(0, 4));
104+
int month = stoi(date.substr(5, 7));
105+
int day = stoi(date.substr(8));
106+
int d = year % 400 == 0 || (year % 4 == 0 && year % 100 != 0) ? 29 : 28;
107+
int days[] = {31, d, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
108+
int ans = day;
109+
for (int i = 0; i < month - 1; ++i) ans += days[i];
110+
return ans;
111+
}
112+
};
113+
```
114+
115+
### **Go**
116+
117+
```go
118+
func dayOfYear(date string) int {
119+
year, _ := strconv.Atoi(date[:4])
120+
month, _ := strconv.Atoi(date[5:7])
121+
day, _ := strconv.Atoi(date[8:])
122+
days := []int{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
123+
if year%400 == 0 || (year%4 == 0 && year%100 != 0) {
124+
days[1]++
125+
}
126+
ans := day
127+
for i := 0; i < month-1; i++ {
128+
ans += days[i]
129+
}
130+
return ans
131+
}
132+
```
96133

134+
### **JavaScript**
135+
136+
```js
137+
/**
138+
* @param {string} date
139+
* @return {number}
140+
*/
141+
var dayOfYear = function(date) {
142+
const year = +date.slice(0, 4);
143+
const month = +date.slice(5, 7);
144+
const day = +date.slice(8);
145+
const d = year % 400 === 0 || (year % 4 === 0 && year % 100 !== 0) ? 29 : 28;
146+
const days = [31, d, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
147+
let ans = day;
148+
for (let i = 0; i < month - 1; ++i) {
149+
ans += days[i];
150+
}
151+
return ans;
152+
};
97153
```
98154

99155
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
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];
11+
return ans;
12+
}
13+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +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:])
5+
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]++
8+
}
9+
ans := day
10+
for i := 0; i < month-1; i++ {
11+
ans += days[i]
12+
}
13+
return ans
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
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) {
10+
ans += days[i];
11+
}
12+
return ans;
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
var dayOfYear = function (date) {
2-
var year = date.substr(0, 4) - 0;
3-
var month = date.substr(5, 2) - 0;
4-
var day = date.substr(8, 2) - 0;
5-
var dayNumber = 0;
6-
for (var m = 1; m < month; m++) {
7-
if (m == 4 || m == 6 || m == 9 || m == 11) {
8-
dayNumber += 30;
9-
} else if (m == 2) {
10-
dayNumber += year % 4 == 0 && year != 1900 ? 29 : 28;
11-
} else {
12-
dayNumber += 31;
1+
/**
2+
* @param {string} date
3+
* @return {number}
4+
*/
5+
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 = year % 400 === 0 || (year % 4 === 0 && year % 100 !== 0) ? 29 : 28;
10+
const days = [31, d, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
11+
let ans = day;
12+
for (let i = 0; i < month - 1; ++i) {
13+
ans += days[i];
1314
}
14-
}
15-
return (dayNumber += day);
16-
};
15+
return ans;
16+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
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 (
5+
year % 4 == 0 and year % 100 != 0) else 28
6+
days = [31, d, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
7+
return sum(days[: month - 1]) + day

0 commit comments

Comments
 (0)