-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1118-NumberOfDaysInAMonth.go
More file actions
55 lines (47 loc) · 1.25 KB
/
1118-NumberOfDaysInAMonth.go
File metadata and controls
55 lines (47 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
// 1118. Number of Days in a Month
// Given a year year and a month month, return the number of days of that month.
// Example 1:
// Input: year = 1992, month = 7
// Output: 31
// Example 2:
// Input: year = 2000, month = 2
// Output: 29
// Example 3:
// Input: year = 1900, month = 2
// Output: 28
// Constraints:
// 1583 <= year <= 2100
// 1 <= month <= 12
import "fmt"
func numberOfDays(year int, month int) int {
isLeapYear := func(year int) bool { // 判断是否是闰年
return year % 4 == 0 && year % 100 != 0 || year % 400 == 0
}
switch month {
case 2:
if isLeapYear(year) { // 处理一下闰年的问题即可
return 29
}
return 28
case 1,3,5,7,8,10,12:
return 31
case 4,6,9,11:
return 30
}
return -1
}
func main() {
// Example 1:
// Input: year = 1992, month = 7
// Output: 31
fmt.Println("1992-7 => ", numberOfDays(1992,7)) // 31
// Example 2:
// Input: year = 2000, month = 2
// Output: 29
fmt.Println("2000-2 => ", numberOfDays(2000,2)) // 29
// Example 3:
// Input: year = 1900, month = 2
// Output: 28
fmt.Println("1900-2 => ", numberOfDays(1900,2)) // 28
}