[LeetCode] 学生出勤记录 I

154 阅读1分钟

学生出勤记录 I

解题思路

通过变量 absent 记录缺勤天数是否超过两天 通过变量 late 记录连续迟到天数 注意: 试了下测试用例 'LLALL' 不算连续迟到三天

代码

var checkRecord = function (s) {
  const len = s.length
  if (len < 2) return true
  let absent = 0
  let late = 0
  for (const c of s) {
    if (c === 'L') {
      late++
      if (late >= 3) return false
    } else {
      late = 0
      if (c === 'A') {
        absent++
        if (absent >= 2) return false
      }
    }
  }
  return true
}