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
            

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

  • 相关阅读:
    jenkins 安装插件失败
    win10 右键新建卡顿
    Linux发布java jar包
    Linux搭建java环境
    java代码检出打包
    虚拟机Vmware使用记录
    地图坐标
    vs2019 扩展工具
    服务器内网穿透
    intelliJ 软件项目打开运行
  • 原文地址:https://www.cnblogs.com/bonelee/p/8727447.html
Copyright © 2011-2022 走看看