0
点赞
收藏
分享

微信扫一扫

LeetCode-Student Attendance Record I


Description:
You are given a string representing an attendance record for a student. The record only contains the following three characters:

'A' : Absent.
'L' : Late.
'P' : Present.

A student could be rewarded if his attendance record doesn’t contain more than one ‘A’ (absent) or more than two continuous ‘L’ (late).

You need to return whether the student could be rewarded according to his attendance record.

Example 1:

Input: "PPALLP"
Output: True

Example 2:

Input: "PPALLL"
Output: False

题意:给定一个字符串记录学生的考勤情况;有下面的三种情况

  • ‘A’ : 缺席
  • ‘L’ : 迟到
  • ‘P’ : 到场

有下列情况的有奖励:A的次数不超过1次或者L连续出现的次数不超过2次;

解法:对于A的出现次数我们直接统计就可以了,现在需要考虑的是连续出现的L的次数,每当L中止连续出现的时候,应当重新计数;

class Solution {
public boolean checkRecord(String s) {
int cntA = 0;
int cntL = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'A') {
cntA++;
} else if (s.charAt(i) == 'L') {
if (cntL > 0 && s.charAt(i - 1) == 'L') {
cntL++;
} else {
cntL = 1;
}
}
if (cntL > 2 || cntA > 1) {
return false;
}
}
return true;
}
}


举报

相关推荐

0 条评论