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:
Example 2:
思路:
本题思路很简单就是简单的逻辑判断,遍历数组,维护两个变量,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;
}
};
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;
}
};
相关文章推荐
- 551. Student Attendance Record I
- String-551-Student Attendance Record I
- 551. Student Attendance Record I
- 551. Student Attendance Record I
- 551. Student Attendance Record I
- leetcode551: Student Attendance Record I
- 551. Student Attendance Record I
- leetcode 551 Student Attendance Record I C++
- 551. Student Attendance Record I
- Leetcode 551 Student Attendance Record I
- String:551. Student Attendance Record I
- Leetcode 551 Student Attendance Record I
- 551. Student Attendance Record I
- leetcode 551[easy]-- Student Attendance Record I
- 551. Student Attendance Record I
- 551. Student Attendance Record I
- 551. Student Attendance Record I
- 551. Student Attendance Record I
- 551. Student Attendance Record I
- 第十周:( LeetCode552) Student Attendance Record II(c++)