Skip to content

Commit

Permalink
✨feat: Add 1154
Browse files Browse the repository at this point in the history
  • Loading branch information
SharingSource committed Dec 21, 2021
1 parent 719f243 commit 4048d2b
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
1 change: 1 addition & 0 deletions Index/前缀和.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
| [930. 和相同的二元子数组](https://leetcode-cn.com/problems/binary-subarrays-with-sum/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/binary-subarrays-with-sum/solution/gong-shui-san-xie-yi-ti-shuang-jie-qian-hfoc0/) | 中等 | 🤩🤩🤩 |
| [1004. 最大连续1的个数 III](https://leetcode-cn.com/problems/max-consecutive-ones-iii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/max-consecutive-ones-iii/solution/san-chong-jie-fa-cong-dong-tai-gui-hua-d-gxks/) | 中等 | 🤩🤩🤩 |
| [1074. 元素和为目标值的子矩阵数量](https://leetcode-cn.com/problems/number-of-submatrices-that-sum-to-target/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/number-of-submatrices-that-sum-to-target/solution/gong-shui-san-xie-you-hua-mei-ju-de-ji-b-uttw/) | 困难 | 🤩🤩🤩 |
| [1154. 一年中的第几天](https://leetcode-cn.com/problems/day-of-the-year/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/day-of-the-year/solution/gong-shui-san-xie-jian-dan-qian-zhui-he-lwo2g/) | 简单 | 🤩🤩🤩🤩 |
| [1208. 尽可能使字符串相等](https://leetcode-cn.com/problems/get-equal-substrings-within-budget/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/get-equal-substrings-within-budget/solution/ni-bu-ke-neng-kan-bu-dong-de-qian-zhui-h-u4l1/) | 中等 | 🤩🤩🤩 |
| [1310. 子数组异或查询](https://leetcode-cn.com/problems/xor-queries-of-a-subarray/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/xor-queries-of-a-subarray/solution/gong-shui-san-xie-yi-ti-shuang-jie-shu-z-rcgu/) | 中等 | 🤩🤩🤩🤩 |
| [1442. 形成两个异或相等数组的三元组数目](https://leetcode-cn.com/problems/count-triplets-that-can-form-two-arrays-of-equal-xor/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/count-triplets-that-can-form-two-arrays-of-equal-xor/solution/gong-shui-san-xie-xiang-jie-shi-yong-qia-7gzm/) | 中等 | 🤩🤩🤩 |
Expand Down
90 changes: 90 additions & 0 deletions LeetCode/1151-1160/1154. 一年中的第几天(简单).md
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 原题链接和其他优选题解。

0 comments on commit 4048d2b

Please sign in to comment.