首页 > 编程语言 > 详细

【数组】551. 学生出勤记录 I

时间:2020-05-04 18:10:24      阅读:55      评论:0      收藏:0      [点我收藏+]

题目:

技术分享图片

 

 

解答:

依次扫描字符串,并用两个计数器记录A和L的数量,其中对于L计数器,因为需要是连续计数,所以如果碰到A和P,则需要重置L计数器。

当A计数器 > 1 时,直接返回false。当L计数器 > 2 时,直接返回false。

都扫描结束,则返回true。

 1 class Solution {
 2 public:
 3     bool checkRecord(string s) 
 4     {
 5         int countA = 0;
 6         int countL = 0;
 7         for (int i = 0; i < s.length(); i++) 
 8         {
 9             char c = s[i];
10             if (c == P) 
11             {
12                 countL = 0;
13             } 
14             else 
15             {
16                 if (c == A) 
17                 {
18                     countA++;
19                     if (countA > 1) 
20                     {
21                         return false;
22                     };
23                     countL = 0;
24                 } 
25                 else if (c == L)
26                 {
27                     countL++;
28                     if (countL > 2) 
29                     {
30                         return false;
31                     };
32                 };
33             };
34         };
35         return true;
36     }
37 };

 

【数组】551. 学生出勤记录 I

原文:https://www.cnblogs.com/ocpc/p/12827394.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!