551. Student Attendance Record I

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

题目:

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

Example 2:

Input: "PPALLL"
Output: False


思路:

本题思路很简单就是简单的逻辑判断,遍历数组,维护两个变量,count_A,count_L,如果当前字符是‘A',count_A++,如果当前字符是’L',count_L++,如果当前字符是A,P,count_L置0。如果count_A>1或count_L>2就返回false否则返回true。

代码:

class Solution {
public:
    bool checkRecord(string s) {
        int count_A =0, count_L =0;
        for(int i=0;i<s.size();i++)
        {
            if(s[i]=='A'){
                count_A++;
                count_L = 0;
            }
            if(s[i]=='P')
                count_L=0;
            if(s[i]=='L')
                count_L++;
            if(count_A>1||count_L>2)
                return false;
        }
        return true;
    }
};



阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6