zoukankan      html  css  js  c++  java
  • leetCode题解 Student Attendance Record I

    1、题目描述

         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’返回false。

    2、代码
     1 bool checkRecord(string s) {
     2         
     3         int numA = 0;
     4         int numL = 0;
     5         for(int i = 0; i < s.size(); i++)
     6         {
     7             if(s[i] == 'A' && ++numA > 1)
     8                 return false;
     9             
    10             if(s[i] == 'L')
    11             {
    12                 numL++;
    13                 if(numL > 2)
    14                     return false;
    15             }
    16                
    17             else 
    18                 numL = 0;
    19         }
    20         return true;
    21     }
    22     



    pp
  • 相关阅读:
    扫雷游戏

    set
    map
    认识了个外国友人!
    插入排序算法
    复习 C语言
    有关于我的一点想法
    linux下的内存分布
    C语言-将输入的字符输出并将多个空格按照一个空格的方式输出
  • 原文地址:https://www.cnblogs.com/wangxiaoyong/p/8665020.html
Copyright © 2011-2022 走看看