leetCode题解 Student Attendance Record I

1、题目描述

     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

输入一个string ,如果其中连续出现出现两次 ‘A’,或者连续出现三次 ‘L’返回false。

2、代码
 1 bool checkRecord(string s) {
 2         
 3         int numA = 0;
 4         int numL = 0;
 5         for(int i = 0; i < s.size(); i++)
 6         {
 7             if(s[i] == 'A' && ++numA > 1)
 8                 return false;
 9             
10             if(s[i] == 'L')
11             {
12                 numL++;
13                 if(numL > 2)
14                     return false;
15             }
16                
17             else 
18                 numL = 0;
19         }
20         return true;
21     }
22     

 




posted @ 2018-03-28 17:16  山里的小勇子  阅读(205)  评论(0编辑  收藏  举报