551. 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.

 

如果字符串中包含的A超过1或者有连续2个以上的L,就输出false

 

C++(3ms):

 1 class Solution {
 2 public:
 3     bool checkRecord(string s) {
 4         int numA = 0 ;
 5         int numL = 0 ;
 6         for (auto t : s){
 7             if (t == 'A')
 8                 numA++ ;
 9             if(t == 'L')
10                 numL++;
11             else
12                 numL = 0 ;
13             if(numA>1 || numL>2)
14                 return false ;
15         }
16         return true ;
17     }
18 };

 

posted @ 2017-06-19 19:02  __Meng  阅读(170)  评论(0编辑  收藏  举报