[LeetCode] Student Attendance Record I

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

判断字符串中是否含有超过1次的‘A‘,或者连续3次’L‘,如果是返回false,否则返回true。遍历字符串数组的同时判断即可。

class Solution {
public:
    bool checkRecord(string s) {
        int A = 0, L = 0;
        for (int i = 0; i != s.size(); i++) {
            if (s[i] == ‘A‘)
                A++;
            if (A > 1)
                return false;
            if (s[i] == ‘L‘ && s[i + 1] == ‘L‘ && s[i + 2] == ‘L‘)
                return false;
        }
        return true;
    }
};
// 3 ms
时间: 2024-10-10 15:15:33

[LeetCode] Student Attendance Record I的相关文章

551. Student Attendance Record I【easy】

551. Student Attendance Record I[easy] 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 attend

【leetcode_easy】551. Student Attendance Record I

problem 551. Student Attendance Record I 参考 1. Leetcode_easy_551. Student Attendance Record I; 完 原文地址:https://www.cnblogs.com/happyamyhope/p/10947954.html

551. Student Attendance Record I(LeetCode)

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 o

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

题目: 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 th

551. 学生出席记录 Student Attendance Record I

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 o

551 Student Attendance Record I

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 o

552. Student Attendance Record II

Problem refer: https://leetcode.com/problems/student-attendance-record-ii/description // My solution: // A good mathmatical derivation problem. // But it makes me confused with the word: "more than two continuous 'L' (late)". // And finally find

Student Attendance Record I

这道题为简单题 题目: 思路: 1.这道题主要搞清楚两种情况就行了: (1).如果'A'出现两次 (2).如果出现'LLL' 以上两种情况出现任意一种就返回False 代码: 我的代码: 1 class Solution(object): 2 def checkRecord(self, s): 3 """ 4 :type s: str 5 :rtype: bool 6 """ 7 a = 0 8 b = 0 9 for i in s: 10 if

Leetcode-552 Student Attendance Record II(学生出勤记录 II)

1 #define maxn 1000000 2 #define _for(i,a,b) for(int i = (a);i < (b);i ++) 3 #define pb push_back 4 #define MOD 1000000007 5 6 class Solution 7 { 8 public: 9 int checkRecord(int n) 10 { 11 long long int dp[2][2][3] {0}; 12 13 dp[0][0][0] = dp[0][0][1