zoukankan      html  css  js  c++  java
  • leetcode 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:

    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
    
    class Solution(object):
        def checkRecord(self, s):
            """
            :type s: str
            :rtype: bool
            """
            return not(s.count('A') > 1 or s.count('LLL') > 0)

    使用计数器:

    class Solution(object):
        def checkRecord(self, s):
            """
            :type s: str
            :rtype: bool
            """
            a = l = 0
            for c in s:
                if c == 'A':
                    a += 1
                    l = 0
                elif c == 'L':
                    l += 1
                else:
                    l = 0
                if a > 1 or l >= 3:
                    return False
            return True
            

    注意黄色部分代码,重置计数器!

  • 相关阅读:
    .NET 4.6.1 给cookie添加属性
    Blog目录
    1019 数字黑洞
    1018 锤子剪刀布
    1017 A除以B
    1016 部分A+B
    1015 德才论
    1014 福尔摩斯的约会
    1013 数素数
    1012 数字分类
  • 原文地址:https://www.cnblogs.com/bonelee/p/8727447.html
Copyright © 2011-2022 走看看