Skip to content

Latest commit

 

History

History
23 lines (21 loc) · 815 Bytes

1185.一周中的第几天.md

File metadata and controls

23 lines (21 loc) · 815 Bytes

方法一:模拟

class Solution {
    public String dayOfTheWeek(int day, int month, int year) {
        String[] dates = new String[]{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
        int[] nums = new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        int res = 4;
        for (int i = 1971; i < year; i++) {
            boolean isLeap = (i % 4 == 0 && i % 100 != 0) || i % 400 == 0;
            res += isLeap ? 366 : 365;
        }
        for (int i = 0; i < month - 1; i++) {
            res += nums[i];
            if (i == 1 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)) res++;
        }
        res += day;
        return dates[res % 7];
    }
}

判断一年是否是闰年:(year % 4 == 0 && year % 100 != 0) || year % 400 == 0