1154-一年中的第几天

Raphael Liu Lv10

给你一个字符串 date ,按 YYYY-MM-DD 格式表示一个
现行公元纪年法 日期。返回该日期是当年的第几天。

示例 1:

**输入:** date = "2019-01-09"
**输出:** 9
**解释:** 给定日期是2019年的第九天。

示例 2:

**输入:** date = "2019-02-10"
**输出:** 41

提示:

  • date.length == 10
  • date[4] == date[7] == '-',其他的 date[i] 都是数字
  • date 表示的范围从 1900 年 1 月 1 日至 2019 年 12 月 31 日

方法一:直接计算

思路与算法

我们首先从给定的字符串 date 中提取出年 year,月 month 以及日 day。

这样一来,我们就可以首先统计到 month 的前一个月为止的天数。这一部分只需要使用一个长度为 12 的数组,预先记录每一个月的天数,再进行累加即可。随后我们将答案再加上 day,就可以得到 date 是一年中的第几天。

需要注意的是,如果 year 是闰年,那么二月份会多出一天。闰年的判定方法为:year 是 400 的倍数,或者 year 是 4 的倍数且不是 100 的倍数。

代码

[sol1-C++]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public:
int dayOfYear(string date) {
int year = stoi(date.substr(0, 4));
int month = stoi(date.substr(5, 2));
int day = stoi(date.substr(8, 2));

int amount[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
++amount[1];
}

int ans = 0;
for (int i = 0; i < month - 1; ++i) {
ans += amount[i];
}
return ans + day;
}
};
[sol1-Java]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public int dayOfYear(String date) {
int year = Integer.parseInt(date.substring(0, 4));
int month = Integer.parseInt(date.substring(5, 7));
int day = Integer.parseInt(date.substring(8));

int[] amount = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
++amount[1];
}

int ans = 0;
for (int i = 0; i < month - 1; ++i) {
ans += amount[i];
}
return ans + day;
}
}
[sol1-C#]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class Solution {
public int DayOfYear(string date) {
int year = int.Parse(date.Substring(0, 4));
int month = int.Parse(date.Substring(5, 2));
int day = int.Parse(date.Substring(8));

int[] amount = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
++amount[1];
}

int ans = 0;
for (int i = 0; i < month - 1; ++i) {
ans += amount[i];
}
return ans + day;
}
}
[sol1-Python3]
1
2
3
4
5
6
7
8
9
10
class Solution:
def dayOfYear(self, date: str) -> int:
year, month, day = [int(x) for x in date.split("-")]

amount = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0):
amount[1] += 1

ans = sum(amount[:month - 1])
return ans + day
[sol1-C]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int dayOfYear(char * date){
int year = atoi(date);
int month = atoi(date + 5);
int day = atoi(date + 8);
int amount[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
++amount[1];
}
int ans = 0;
for (int i = 0; i < month - 1; ++i) {
ans += amount[i];
}
return ans + day;
}
[sol1-Golang]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
func dayOfYear(date string) int {
year, _ := strconv.Atoi(date[:4])
month, _ := strconv.Atoi(date[5:7])
day, _ := strconv.Atoi(date[8:])

days := []int{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
if year%400 == 0 || (year%4 == 0 && year%100 != 0) {
days[1]++
}

ans := day
for _, d := range days[:month-1] {
ans += d
}
return ans
}
[sol1-JavaScript]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
var dayOfYear = function(date) {
const year = +date.slice(0, 4);
const month = +date.slice(5, 7);
const day = +date.slice(8);

const amount = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
if (year % 400 === 0 || (year % 4 === 0 && year % 100 !== 0)) {
++amount[1];
}

let ans = 0;
for (let i = 0; i < month - 1; ++i) {
ans += amount[i];
}
return ans + day;
};

复杂度分析

  • 时间复杂度:O(1)。我们将字符串的长度(定值 7)以及一年的月份数 12 视为常数。

  • 空间复杂度:O(1)。

 Comments
On this page
1154-一年中的第几天