题目地址:
https://leetcode.com/problems/day-of-the-year/
给定一个日期,形式为YYYY-MM-DD
,判断这一天是这一年的第几天。
代码如下:
public class Solution {
public int dayOfYear(String date) {
String[] ss = date.split("-");
int[] days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (isLeap(Integer.parseInt(ss[0]))) {
days[1] = 29;
}
int month = Integer.parseInt(ss[1]), day = Integer.parseInt(ss[2]), res = 0;
for (int i = 0; i < month - 1; i++) {
res += days[i];
}
return res + day;
}
boolean isLeap(int year) {
return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
}
}
时空复杂度 O ( 1 ) O(1) O(1)。