LeetCode 551 Student Attendance Record I

来源:互联网 发布:韩子高网络剧有下部吗 编辑:程序博客网 时间:2024/06/05 18:04

题目:

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
题目链接

题意:

有一个学生出勤表,表中仅有三种情况”A“代表缺席,”L“代表迟到,”P“代表在场,评奖要求缺席次数不超过1次,且不得连续迟到两次以上,否则取消评奖资格,先给一个出勤表,问该学生是否有资格获奖。

利用变量统计次数即可。

代码如下:

class Solution {public:    bool checkRecord(string s) {        map<char, int> dic;        for (char c : s) {            dic[c] ++;            if (c != 'L') dic['L'] = 0;            if (dic['A']>1 || dic['L']>2) return false;        }        return true;    }};


原创粉丝点击