Skip to content

Commit e727ba5

Browse files
Merge pull request SharingSource#94 from SharingSource/ac_oier
✨feat: Add 551
2 parents 0b8515c + 93c0d9f commit e727ba5

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

Index/模拟.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
| [413. 等差数列划分](https://leetcode-cn.com/problems/arithmetic-slices/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/arithmetic-slices/solution/gong-shui-san-xie-shuang-zhi-zhen-qiu-ji-ef1q/) | 中等 | 🤩🤩🤩🤩 |
2424
| [451. 根据字符出现频率排序](https://leetcode-cn.com/problems/sort-characters-by-frequency/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/sort-characters-by-frequency/solution/gong-shui-san-xie-shu-ju-jie-gou-yun-yon-gst9/) | 中等 | 🤩🤩🤩🤩 |
2525
| [457. 环形数组是否存在循环](https://leetcode-cn.com/problems/circular-array-loop/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/circular-array-loop/solution/gong-shui-san-xie-yi-ti-shuang-jie-mo-ni-ag05/) | 中等 | 🤩🤩🤩🤩 |
26+
| [551. 学生出勤记录 I](https://leetcode-cn.com/problems/student-attendance-record-i/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/student-attendance-record-i/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-hui7/) | 简单 | 🤩🤩🤩 |
2627
| [566. 重塑矩阵](https://leetcode-cn.com/problems/reshape-the-matrix/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/reshape-the-matrix/solution/jian-dan-ti-zhong-quan-chu-ji-ke-yi-kan-79gv5/) | 简单 | 🤩🤩🤩 |
2728
| [645. 错误的集合](https://leetcode-cn.com/problems/set-mismatch/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/set-mismatch/solution/gong-shui-san-xie-yi-ti-san-jie-ji-shu-s-vnr9/) | 简单 | 🤩🤩🤩 |
2829
| [726. 原子的数量](https://leetcode-cn.com/problems/number-of-atoms/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/number-of-atoms/solution/gong-shui-san-xie-shi-yong-xiao-ji-qiao-l5ak4/) | 困难 | 🤩🤩🤩🤩 |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[551. 学生出勤记录 I](https://leetcode-cn.com/problems/student-attendance-record-i/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-hui7/)** ,难度为 **中等**
4+
5+
Tag : 「模拟」
6+
7+
给你一个字符串 s 表示一个学生的出勤记录,其中的每个字符用来标记当天的出勤情况(缺勤、迟到、到场)。记录中只含下面三种字符:
8+
* 'A':Absent,缺勤
9+
* 'L':Late,迟到
10+
* 'P':Present,到场
11+
12+
如果学生能够 同时 满足下面两个条件,则可以获得出勤奖励:
13+
14+
按 总出勤 计,学生缺勤('A')严格 少于两天。
15+
学生 不会 存在 连续 3 天或 3 天以上的迟到('L')记录。
16+
如果学生可以获得出勤奖励,返回 true ;否则,返回 false 。
17+
18+
示例 1:
19+
```
20+
输入:s = "PPALLP"
21+
22+
输出:true
23+
24+
解释:学生缺勤次数少于 2 次,且不存在 3 天或以上的连续迟到记录。
25+
```
26+
示例 2:
27+
```
28+
输入:s = "PPALLL"
29+
30+
输出:false
31+
32+
解释:学生最后三天连续迟到,所以不满足出勤奖励的条件。
33+
```
34+
35+
提示:
36+
* 1 <= s.length <= 1000
37+
* s[i] 为 'A'、'L' 或 'P'
38+
39+
40+
---
41+
42+
### 模拟
43+
44+
放大假,根据题意进行模拟即可。
45+
46+
代码:
47+
```Java
48+
class Solution {
49+
public boolean checkRecord(String s) {
50+
int n = s.length();
51+
char[] cs = s.toCharArray();
52+
for (int i = 0, cnt = 0; i < n; ) {
53+
char c = cs[i];
54+
if (c == 'A') {
55+
cnt++;
56+
if (cnt >= 2) return false;
57+
} else if (c == 'L') {
58+
int j = i;
59+
while (j < n && cs[j] == 'L') j++;
60+
int len = j - i;
61+
if (len >= 3) return false;
62+
i = j;
63+
continue;
64+
}
65+
i++;
66+
}
67+
return true;
68+
}
69+
}
70+
```
71+
* 时间复杂度:$O(n)$
72+
* 空间复杂度:使用 `charAt` 代替 `toCharArray` 的话为 $O(1)$,否则为 $O(n)$
73+
74+
### 最后
75+
76+
这是我们「刷穿 LeetCode」系列文章的第 `No.551` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
77+
78+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
79+
80+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode。
81+
82+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
83+

0 commit comments

Comments
 (0)