Leetcode - Lonely Pixel I
Leetcode - Construct Binary Search Tree from Preorder Traversal

Leetcode - Student Attendance Record I

violet posted @ Apr 18, 2020 08:10:27 AM in 算法 with tags Algorithm Golang , 194 阅读

https://leetcode.com/problems/student-attendance-record-i/

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

  1. 'A' : Absent.
  2. 'L' : Late.
  3. '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

 

Yet another shitty question.

func checkRecord(s string) bool {
    a := 0
    l := 0
    for i := 0; i < len(s); i++ {
        if s[i] == 'A' {
            a++
            l = 0
        }
        if s[i] == 'L' {
            l++
        }
        if s[i] == 'P' {
            l = 0
        }
        if a > 1 || l > 2 {
            return false
        }
    }
    
    return true
}

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter