您的位置:首页 > 编程语言 > C语言/C++

551. Student Attendance Record I

2017-08-26 09:30 417 查看
题目:

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

'A' : Absent.
'L' : Late.
'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;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++ leetcode