-
Notifications
You must be signed in to change notification settings - Fork 956
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
719f243
commit 4048d2b
Showing
2 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
### 题目描述 | ||
|
||
这是 LeetCode 上的 **[1154. 一年中的第几天](https://leetcode-cn.com/problems/day-of-the-year/solution/gong-shui-san-xie-jian-dan-qian-zhui-he-lwo2g/)** ,难度为**简单**。 | ||
|
||
Tag : 「前缀和」、「模拟」 | ||
|
||
|
||
|
||
给你一个字符串 `date`,按 `YYYY-MM-DD` 格式表示一个 现行公元纪年法 日期。 | ||
|
||
请你计算并返回该日期是当年的第几天。 | ||
|
||
通常情况下,我们认为 `1` 月 `1` 日是每年的第 `1` 天,`1` 月 `2` 日是每年的第 `2` 天,依此类推。每个月的天数与现行公元纪年法(格里高利历)一致。 | ||
|
||
示例 1: | ||
``` | ||
输入:date = "2019-01-09" | ||
输出:9 | ||
``` | ||
示例 2: | ||
``` | ||
输入:date = "2019-02-10" | ||
输出:41 | ||
``` | ||
示例 3: | ||
``` | ||
输入:date = "2003-03-01" | ||
输出:60 | ||
``` | ||
示例 4: | ||
``` | ||
输入:date = "2004-03-01" | ||
输出:61 | ||
``` | ||
|
||
提示: | ||
* `date.length == 10` | ||
* `date[4] == date[7] == '-'`,其他的 `date[i]` 都是数字 | ||
* `date` 表示的范围从 `1900` 年 `1` 月 `1` 日至 `2019` 年 `12` 月 `31` 日 | ||
|
||
--- | ||
|
||
### 模拟 | ||
|
||
根据题意进行模拟即可。 | ||
|
||
由于每年月份的天数都相对固定(仅有闰年 $2$ 月份不同),因此我们可以使用 `static` 进行打表预处理。 | ||
|
||
使用 `nums` 构建一个平年的月份数组,再通过「前缀和」得到平年的每个月的天数总和。 | ||
|
||
最后将 `date` 按照 `-` 分割为年月日,分别进行计数。 | ||
|
||
注意,当年份为闰年且当前月份超过了 $2$ 月份,要追加一天。 | ||
|
||
代码: | ||
```Java | ||
class Solution { | ||
static int[] nums = new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; | ||
static int[] f = new int[13]; | ||
static { | ||
for (int i = 1; i <= 12; i++) f[i] = f[i - 1] + nums[i - 1]; | ||
} | ||
public int dayOfYear(String date) { | ||
String[] ss = date.split("-"); | ||
int y = Integer.parseInt(ss[0]), m = Integer.parseInt(ss[1]), d = Integer.parseInt(ss[2]); | ||
boolean isLeap = (y % 4 == 0 && y % 100 != 0) || y % 400 == 0; | ||
int ans = m > 2 && isLeap ? f[m - 1] + 1 : f[m - 1]; | ||
return ans + d; | ||
} | ||
} | ||
``` | ||
* 时间复杂度:$O(1)$ | ||
* 空间复杂度:$O(1)$ | ||
|
||
--- | ||
|
||
### 最后 | ||
|
||
这是我们「刷穿 LeetCode」系列文章的第 `No.1154` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 | ||
|
||
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 | ||
|
||
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。 | ||
|
||
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。 | ||
|