LeetCode 551:Student Attendance Record I (c++)

来源:互联网 发布:网一网络加速器官网 编辑:程序博客网 时间:2024/05/20 05:24

一:题目

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) ormore 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

二:解题思路

学生受到惩罚:

1.大于1个A

2.或者有连续2个以上的L

通过统计A的个数,和查看是否有连续3个L来解决

三:代码实现

class Solution {public:    bool checkRecord(string s) {        //学生受到惩罚:大于1个A,或者,大于2个连续的L                int numA=0;        int i;                            for(i=0;i<s.length();i++){            if(s[i]=='A')                numA++;            if(numA>1)                return false;            if(i>=2 && s[i]=='L' && s[i-1]=='L' && s[i-2]=='L')                return false;        }                 return true;    }};


原创粉丝点击