Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 05743f3

Browse files
✨feat: Add 1185
1 parent bab4512 commit 05743f3

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

‎Index/模拟.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
| [1078. Bigram 分词](https://leetcode-cn.com/problems/occurrences-after-bigram/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/occurrences-after-bigram/solution/gong-shui-san-xie-jian-dan-zi-fu-chuan-m-qyki/) | 简单 | 🤩🤩🤩🤩 |
7676
| [1104. 二叉树寻路](https://leetcode-cn.com/problems/path-in-zigzag-labelled-binary-tree/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/path-in-zigzag-labelled-binary-tree/solution/gong-shui-san-xie-yi-ti-shuang-jie-mo-ni-rw2d/) | 中等 | 🤩🤩🤩 |
7777
| [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/) | 简单 | 🤩🤩🤩🤩 |
78+
| [1185. 一周中的第几天](https://leetcode-cn.com/problems/day-of-the-week/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/day-of-the-week/solution/gong-shui-san-xie-jian-dan-ri-qi-tong-ji-czt6/) | 简单 | 🤩🤩🤩🤩 |
7879
| [1436. 旅行终点站](https://leetcode-cn.com/problems/destination-city/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/destination-city/solution/gong-shui-san-xie-jian-dan-fang-jia-mo-n-y47c/) | 简单 | 🤩🤩🤩🤩🤩 |
7980
| [1446. 连续字符](https://leetcode-cn.com/problems/consecutive-characters/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/consecutive-characters/solution/gong-shui-san-xie-jian-dan-shuang-zhi-zh-xtv6/) | 简单 | 🤩🤩🤩🤩🤩 |
8081
| [1480. 一维数组的动态和](https://leetcode-cn.com/problems/running-sum-of-1d-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/running-sum-of-1d-array/solution/gong-shui-san-xie-yi-wei-qian-zhui-he-mo-g8hn/) | 简单 | 🤩🤩🤩🤩🤩 |
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[1185. 一周中的第几天](https://leetcode-cn.com/problems/day-of-the-week/solution/gong-shui-san-xie-jian-dan-ri-qi-tong-ji-czt6/)** ,难度为 **简单**
4+
5+
Tag : 「模拟」
6+
7+
8+
9+
给你一个日期,请你设计一个算法来判断它是对应一周中的哪一天。
10+
11+
输入为三个整数:`day``month``year`,分别表示日、月、年。
12+
13+
您返回的结果必须是这几个值中的一个 `{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}`
14+
15+
示例 1:
16+
```
17+
输入:day = 31, month = 8, year = 2019
18+
19+
输出:"Saturday"
20+
```
21+
示例 2:
22+
```
23+
输入:day = 18, month = 7, year = 1999
24+
25+
输出:"Sunday"
26+
```
27+
示例 3:
28+
```
29+
输入:day = 15, month = 8, year = 1993
30+
31+
输出:"Sunday"
32+
```
33+
34+
提示:
35+
给出的日期一定是在 `1971``2100` 年之间的有效日期。
36+
37+
---
38+
39+
### 模拟
40+
41+
题目保证日期是在 `1971``2100` 之间,我们可以计算给定日期距离 `1970` 的最后一天(星期四)间隔了多少天,从而得知给定日期是周几。
42+
43+
具体的,可以先通过循环处理计算年份在 $[1971, year - 1]$ 时间段,经过了多少天(注意平年为 365ドル,ドル闰年为 366ドル$);然后再处理当前年 $year$ 的月份在 $[1, month - 1]$ 时间段 ,经过了多少天(注意当天年是否为闰年,特殊处理 2ドル$ 月份),最后计算当前月 $month$ 经过了多少天,即再增加 $day$ 天。
44+
45+
得到距离 `1970` 的最后一天(星期四)的天数后进行取模,即可映射回答案。
46+
47+
代码:
48+
```Java
49+
class Solution {
50+
static String[] ss = new String[]{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
51+
static int[] nums = new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
52+
public String dayOfTheWeek(int day, int month, int year) {
53+
int ans = 4;
54+
for (int i = 1971; i < year; i++) {
55+
boolean isLeap = (i % 4 == 0 && i % 100 != 0) || i % 400 == 0;
56+
ans += isLeap ? 366 : 365;
57+
}
58+
for (int i = 1; i < month; i++) {
59+
ans += nums[i - 1];
60+
if (i == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)) ans++;
61+
}
62+
ans += day;
63+
return ss[ans % 7];
64+
}
65+
}
66+
```
67+
* 时间复杂度:令当前时间 `2100-12-xx`,此时达到数据范围对应的计算量上界,设为 $C$。整体复杂度为 $O(C)$
68+
* 空间复杂度:$O(1)$
69+
70+
---
71+
72+
### 最后
73+
74+
这是我们「刷穿 LeetCode」系列文章的第 `No.1185` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
75+
76+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
77+
78+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
79+
80+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
81+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /