year为年份,1971为起始日期
- 先加上所有模 4 为 0 的年份的数量。此时有些模 100 为 0 的不是闰年的年份被加上了。
- 再减去所有模 100 为 0 的年份的数量。此时有些模 400 为 0 的是闰年的年份被减去了。
- 再加上所有模 400 为 0 的年份的数量。完成。
ans += (year - 1) / 4 - 1971 / 4;
ans -= (year - 1) / 100 - 1971 / 100;
ans += (year - 1) / 400 - 1971 / 400;
作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/number-of-days-between-two-dates/solution/ri-qi-zhi-jian-ge-ji-tian-by-leetcode-solution/
来源:力扣(LeetCode)