码迷,mamicode.com
首页 > 编程语言 > 详细

LeetCode 551. Student Attendance Record I (C++)

时间:2019-04-18 00:45:25      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:pre   div   style   ret   you   solution   check   end   ==   

题目:

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,P,如果多于1个A,或者多于连续2个L则返回False,否则返回True。

我们定义Lcnums表示L连续的数量,Anums表示A的数量,对给定的s遍历。

首先判断字符是不是A,如果是则Anums加1,否则什么都不做。继续判断这个字符是不是L,如果是则Lcnums加1,否则将Lcunms清零,注意遍历的时候只要字符不是L都要将Lcnums清零,然后判断Lcnums和Anums的数量是否符合要求即可。

程序:

class Solution {
public:
    bool checkRecord(string s) {
        int Lcnums = 0;
        int Anums = 0;
        for(auto i:s){
            if(i == A) Anums++;
            if(i == L){
                Lcnums++;
            }
            else
                Lcnums = 0;
            if(Anums == 2||Lcnums == 3){
                return false;
            }
        }
        return true;
    }
};

LeetCode 551. Student Attendance Record I (C++)

标签:pre   div   style   ret   you   solution   check   end   ==   

原文地址:https://www.cnblogs.com/silentteller/p/10727145.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!