Skip to content

Commit 2988035

Browse files
authored
Merge pull request halfrost#166 from gostool/leetcode0551
add: leetcode 0551 solution
2 parents 7db2a76 + d0f54e2 commit 2988035

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package leetcode
2+
3+
func checkRecord(s string) bool {
4+
numsA := 0
5+
maxL := 0
6+
numsL := 0
7+
for _, v := range s {
8+
if v == 'L' {
9+
numsL++
10+
} else {
11+
if numsL > maxL {
12+
maxL = numsL
13+
}
14+
numsL = 0
15+
if v == 'A' {
16+
numsA++
17+
}
18+
}
19+
}
20+
if numsL > maxL {
21+
maxL = numsL
22+
}
23+
return numsA < 2 && maxL < 3
24+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question551 struct {
9+
para551
10+
ans551
11+
}
12+
13+
// para 是参数
14+
type para551 struct {
15+
s string
16+
}
17+
18+
// ans 是答案
19+
type ans551 struct {
20+
ans bool
21+
}
22+
23+
func Test_Problem551(t *testing.T) {
24+
25+
qs := []question551{
26+
27+
{
28+
para551{"PPALLP"},
29+
ans551{true},
30+
},
31+
32+
{
33+
para551{"PPALLL"},
34+
ans551{false},
35+
},
36+
}
37+
38+
fmt.Printf("------------------------Leetcode Problem 551------------------------\n")
39+
40+
for _, q := range qs {
41+
_, p := q.ans551, q.para551
42+
fmt.Printf("【input】:%v 【output】:%v \n", p, checkRecord(p.s))
43+
}
44+
fmt.Printf("\n\n\n")
45+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# [551. Student Attendance Record I](https://leetcode-cn.com/problems/student-attendance-record-i/)
2+
3+
## 题目
4+
5+
You are given a string s representing an attendance record for a student where each character signifies whether the student was absent, late, or present on that day. The record only contains the following three characters:
6+
7+
- 'A': Absent.
8+
- 'L': Late.
9+
- 'P': Present.
10+
11+
The student is eligible for an attendance award if they meet both of the following criteria:
12+
13+
The student was absent ('A') for strictly fewer than 2 days total.
14+
15+
The student was never late ('L') for 3 or more consecutive days.
16+
17+
Return true if the student is eligible for an attendance award, or false otherwise.
18+
19+
**Example 1:**
20+
21+
Input: s = "PPALLP"
22+
Output: true
23+
Explanation: The student has fewer than 2 absences and was never late 3 or more consecutive days.
24+
25+
**Example 2:**
26+
27+
Input: s = "PPALLL"
28+
Output: false
29+
Explanation: The student was late 3 consecutive days in the last 3 days, so is not eligible for the award.
30+
31+
**Constraints:**
32+
33+
- 1 <= s.length <= 1000
34+
- s[i] is either 'A', 'L', or 'P'.
35+
36+
## 题目大意
37+
38+
给你一个字符串 s 表示一个学生的出勤记录,其中的每个字符用来标记当天的出勤情况(缺勤、迟到、到场)。记录中只含下面三种字符:
39+
40+
- 'A':Absent,缺勤
41+
- 'L':Late,迟到
42+
- 'P':Present,到场
43+
44+
如果学生能够 同时 满足下面两个条件,则可以获得出勤奖励:
45+
46+
按 总出勤 计,学生缺勤('A')严格 少于两天。
47+
48+
学生 不会 存在 连续 3 天或 3 天以上的迟到('L')记录。
49+
50+
如果学生可以获得出勤奖励,返回 true ;否则,返回 false 。
51+
52+
## 解题思路
53+
54+
- 遍历字符串s求出'A'的总数量和连续'L'的最大数量
55+
- 比较'A'的数量是否小于2并且'L'的连续最大数量是否小于3

0 commit comments

Comments
 (0)