Skip to content

Commit fb9ada3

Browse files
authored
feat: add solutions to lc problem: No.1360 (doocs#1507)
No.1360.Number of Days Between Two Dates
1 parent bac2d3f commit fb9ada3

File tree

7 files changed

+526
-2
lines changed

7 files changed

+526
-2
lines changed

solution/1300-1399/1360.Number of Days Between Two Dates/README.md

Lines changed: 168 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,189 @@
3636

3737
<!-- 这里可写通用的实现逻辑 -->
3838

39+
**方法一:数学**
40+
41+
我们先定义一个函数 `isLeapYear(year)` 来判断给定的年份 `year` 是否是闰年,如果是闰年则返回 `true`,否则返回 `false`
42+
43+
接下来,我们再定义一个函数 `daysInMonth(year, month)` 来计算给定的年份 `year` 和月份 `month` 一共有多少天,我们可以使用一个数组 `days` 来存储每个月份的天数,其中 `days[1]` 表示二月份的天数,如果是闰年则为 $29$ 天,否则为 $28$ 天。
44+
45+
然后,我们再定义一个函数 `calcDays(date)` 来计算给定的日期 `date` 距离 `1971-01-01` 有多少天,我们可以使用 `date.split("-")` 来将日期 `date` 按照 `-` 分割成年份 `year`、月份 `month` 和日期 `day`,然后我们可以使用一个循环来计算从 `1971` 年到 `year` 年一共有多少天,然后再计算从 `1` 月到 `month` 月一共有多少天,最后再加上 `day` 天即可。
46+
47+
最后,我们只需要返回 `calcDays(date1) - calcDays(date2)` 的绝对值即可。
48+
49+
时间复杂度 $O(y + m)$,其中 $y$ 表示给定的日期距离 `1971-01-01` 的年数,而 $m$ 表示给定的日期的月数。空间复杂度 $O(1)$。
50+
3951
<!-- tabs:start -->
4052

4153
### **Python3**
4254

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

4557
```python
46-
58+
class Solution:
59+
def daysBetweenDates(self, date1: str, date2: str) -> int:
60+
def isLeapYear(year: int) -> bool:
61+
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
62+
63+
def daysInMonth(year: int, month: int) -> int:
64+
days = [
65+
31,
66+
28 + int(isLeapYear(year)),
67+
31,
68+
30,
69+
31,
70+
30,
71+
31,
72+
31,
73+
30,
74+
31,
75+
30,
76+
31,
77+
]
78+
return days[month - 1]
79+
80+
def calcDays(date: str) -> int:
81+
year, month, day = map(int, date.split("-"))
82+
days = 0
83+
for y in range(1971, year):
84+
days += 365 + int(isLeapYear(y))
85+
for m in range(1, month):
86+
days += daysInMonth(year, m)
87+
days += day
88+
return days
89+
90+
return abs(calcDays(date1) - calcDays(date2))
4791
```
4892

4993
### **Java**
5094

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

5397
```java
98+
class Solution {
99+
public int daysBetweenDates(String date1, String date2) {
100+
return Math.abs(calcDays(date1) - calcDays(date2));
101+
}
102+
103+
private boolean isLeapYear(int year) {
104+
return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
105+
}
106+
107+
private int daysInMonth(int year, int month) {
108+
int[] days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
109+
days[1] += isLeapYear(year) ? 1 : 0;
110+
return days[month - 1];
111+
}
112+
113+
private int calcDays(String date) {
114+
int year = Integer.parseInt(date.substring(0, 4));
115+
int month = Integer.parseInt(date.substring(5, 7));
116+
int day = Integer.parseInt(date.substring(8));
117+
int days = 0;
118+
for (int y = 1971; y < year; ++y) {
119+
days += isLeapYear(y) ? 366 : 365;
120+
}
121+
for (int m = 1; m < month; ++m) {
122+
days += daysInMonth(year, m);
123+
}
124+
days += day;
125+
return days;
126+
}
127+
}
128+
```
129+
130+
### **C++**
131+
132+
```cpp
133+
134+
```
135+
136+
### **Go**
137+
138+
```go
139+
func daysBetweenDates(date1 string, date2 string) int {
140+
return abs(calcDays(date1) - calcDays(date2))
141+
}
142+
143+
func isLeapYear(year int) bool {
144+
return year%4 == 0 && (year%100 != 0 || year%400 == 0)
145+
}
146+
147+
func daysInMonth(year, month int) int {
148+
days := [12]int{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
149+
if isLeapYear(year) {
150+
days[1] = 29
151+
}
152+
return days[month-1]
153+
}
154+
155+
func calcDays(date string) int {
156+
year, _ := strconv.Atoi(date[:4])
157+
month, _ := strconv.Atoi(date[5:7])
158+
day, _ := strconv.Atoi(date[8:])
159+
days := 0
160+
for y := 1971; y < year; y++ {
161+
days += 365
162+
if isLeapYear(y) {
163+
days++
164+
}
165+
}
166+
for m := 1; m < month; m++ {
167+
days += daysInMonth(year, m)
168+
}
169+
days += day
170+
return days
171+
}
172+
173+
func abs(x int) int {
174+
if x < 0 {
175+
return -x
176+
}
177+
return x
178+
}
179+
```
54180

181+
### **TypeScript**
182+
183+
```ts
184+
function daysBetweenDates(date1: string, date2: string): number {
185+
return Math.abs(calcDays(date1) - calcDays(date2));
186+
}
187+
188+
function isLeapYear(year: number): boolean {
189+
return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
190+
}
191+
192+
function daysOfMonth(year: number, month: number): number {
193+
const days = [
194+
31,
195+
isLeapYear(year) ? 29 : 28,
196+
31,
197+
30,
198+
31,
199+
30,
200+
31,
201+
31,
202+
30,
203+
31,
204+
30,
205+
31,
206+
];
207+
return days[month - 1];
208+
}
209+
210+
function calcDays(date: string): number {
211+
let days = 0;
212+
const [year, month, day] = date.split('-').map(Number);
213+
for (let y = 1971; y < year; ++y) {
214+
days += isLeapYear(y) ? 366 : 365;
215+
}
216+
for (let m = 1; m < month; ++m) {
217+
days += daysOfMonth(year, m);
218+
}
219+
days += day - 1;
220+
return days;
221+
}
55222
```
56223

57224
### **...**

solution/1300-1399/1360.Number of Days Between Two Dates/README_EN.md

Lines changed: 186 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,198 @@
3030
### **Python3**
3131

3232
```python
33-
33+
class Solution:
34+
def daysBetweenDates(self, date1: str, date2: str) -> int:
35+
def isLeapYear(year: int) -> bool:
36+
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
37+
38+
def daysInMonth(year: int, month: int) -> int:
39+
days = [
40+
31,
41+
28 + int(isLeapYear(year)),
42+
31,
43+
30,
44+
31,
45+
30,
46+
31,
47+
31,
48+
30,
49+
31,
50+
30,
51+
31,
52+
]
53+
return days[month - 1]
54+
55+
def calcDays(date: str) -> int:
56+
year, month, day = map(int, date.split("-"))
57+
days = 0
58+
for y in range(1971, year):
59+
days += 365 + int(isLeapYear(y))
60+
for m in range(1, month):
61+
days += daysInMonth(year, m)
62+
days += day
63+
return days
64+
65+
return abs(calcDays(date1) - calcDays(date2))
3466
```
3567

3668
### **Java**
3769

3870
```java
71+
class Solution {
72+
public int daysBetweenDates(String date1, String date2) {
73+
return Math.abs(calcDays(date1) - calcDays(date2));
74+
}
75+
76+
private boolean isLeapYear(int year) {
77+
return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
78+
}
79+
80+
private int daysInMonth(int year, int month) {
81+
int[] days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
82+
days[1] += isLeapYear(year) ? 1 : 0;
83+
return days[month - 1];
84+
}
85+
86+
private int calcDays(String date) {
87+
int year = Integer.parseInt(date.substring(0, 4));
88+
int month = Integer.parseInt(date.substring(5, 7));
89+
int day = Integer.parseInt(date.substring(8));
90+
int days = 0;
91+
for (int y = 1971; y < year; ++y) {
92+
days += isLeapYear(y) ? 366 : 365;
93+
}
94+
for (int m = 1; m < month; ++m) {
95+
days += daysInMonth(year, m);
96+
}
97+
days += day;
98+
return days;
99+
}
100+
}
101+
```
102+
103+
### **C++**
104+
105+
```cpp
106+
class Solution {
107+
public:
108+
int daysBetweenDates(string date1, string date2) {
109+
return abs(calcDays(date1) - calcDays(date2));
110+
}
111+
112+
bool isLeapYear(int year) {
113+
return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
114+
}
115+
116+
int daysInMonth(int year, int month) {
117+
int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
118+
days[1] += isLeapYear(year);
119+
return days[month - 1];
120+
}
121+
122+
int calcDays(string date) {
123+
int year = stoi(date.substr(0, 4));
124+
int month = stoi(date.substr(5, 2));
125+
int day = stoi(date.substr(8, 2));
126+
int days = 0;
127+
for (int y = 1971; y < year; ++y) {
128+
days += 365 + isLeapYear(y);
129+
}
130+
for (int m = 1; m < month; ++m) {
131+
days += daysInMonth(year, m);
132+
}
133+
days += day;
134+
return days;
135+
}
136+
};
137+
```
138+
139+
### **Go**
140+
141+
```go
142+
func daysBetweenDates(date1 string, date2 string) int {
143+
return abs(calcDays(date1) - calcDays(date2))
144+
}
145+
146+
func isLeapYear(year int) bool {
147+
return year%4 == 0 && (year%100 != 0 || year%400 == 0)
148+
}
149+
150+
func daysInMonth(year, month int) int {
151+
days := [12]int{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
152+
if isLeapYear(year) {
153+
days[1] = 29
154+
}
155+
return days[month-1]
156+
}
157+
158+
func calcDays(date string) int {
159+
year, _ := strconv.Atoi(date[:4])
160+
month, _ := strconv.Atoi(date[5:7])
161+
day, _ := strconv.Atoi(date[8:])
162+
days := 0
163+
for y := 1971; y < year; y++ {
164+
days += 365
165+
if isLeapYear(y) {
166+
days++
167+
}
168+
}
169+
for m := 1; m < month; m++ {
170+
days += daysInMonth(year, m)
171+
}
172+
days += day
173+
return days
174+
}
175+
176+
func abs(x int) int {
177+
if x < 0 {
178+
return -x
179+
}
180+
return x
181+
}
182+
```
39183

184+
### **TypeScript**
185+
186+
```ts
187+
function daysBetweenDates(date1: string, date2: string): number {
188+
return Math.abs(calcDays(date1) - calcDays(date2));
189+
}
190+
191+
function isLeapYear(year: number): boolean {
192+
return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
193+
}
194+
195+
function daysOfMonth(year: number, month: number): number {
196+
const days = [
197+
31,
198+
isLeapYear(year) ? 29 : 28,
199+
31,
200+
30,
201+
31,
202+
30,
203+
31,
204+
31,
205+
30,
206+
31,
207+
30,
208+
31,
209+
];
210+
return days[month - 1];
211+
}
212+
213+
function calcDays(date: string): number {
214+
let days = 0;
215+
const [year, month, day] = date.split('-').map(Number);
216+
for (let y = 1971; y < year; ++y) {
217+
days += isLeapYear(y) ? 366 : 365;
218+
}
219+
for (let m = 1; m < month; ++m) {
220+
days += daysOfMonth(year, m);
221+
}
222+
days += day - 1;
223+
return days;
224+
}
40225
```
41226

42227
### **...**

0 commit comments

Comments
 (0)