作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/student-attendance-record-i/#/description

题目描述

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 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的次数不超过1次,连续L的次数不超过2次,那一定是优秀的同学,绝对是优秀的同学。

判断一个学生是不是优秀的同学。

解题方法

正则表达式

一直错的原因是没看清题,题目说的是不超过一个A或者两个连续的L,直接求解有点麻烦,可以用正则表达式。
.*匹配的是任意字符重复0次或者任意次,一行代码搞定。

public class Solution {
public boolean checkRecord(String s) {
return !s.matches(".*A.*A.*") && !s.matches(".*LLL.*");
}
}

python写法如下,注意的是需要把正则表达式写在第一个参数,字符串s作为第二个参数。

class Solution:
def checkRecord(self, s):
"""
:type s: str
:rtype: bool
"""
return not re.match(".*A.*A.*", s) and not re.match(".*LLL.*", s)

统计

直接数A的个数不能超过1,连续的L的个数不能多于2即可。代码还很简单高效的,超过了98%的提交。

class Solution:
def checkRecord(self, s):
"""
:type s: str
:rtype: bool
"""
N = len(s)
s = s + "P"
countA = 0
countL = 0
for i in range(N):
if s[i] == "A":
countA += 1
if countA > 1:
return False
if s[i] == "L":
countL += 1
if countL >= 3:
return False
else:
countL = 0
return True

日期

2017 年 4 月 21 日
2018 年 11 月 17 日 —— 美妙的周末,美丽的天气

【LeetCode】551. Student Attendance Record I 解题报告(Java & Python)的更多相关文章

  1. LeetCode 551. Student Attendance Record I (学生出勤纪录 I)

    You are given a string representing an attendance record for a student. The record only contains the ...

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

    题目: You are given a string representing an attendance record for a student. The record only contains ...

  3. 【LeetCode】459. Repeated Substring Pattern 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历子串 日期 [LeetCode] 题目地址:ht ...

  4. 551. Student Attendance Record I【easy】

    551. Student Attendance Record I[easy] You are given a string representing an attendance record for ...

  5. 【leetcode_easy】551. Student Attendance Record I

    problem 551. Student Attendance Record I 题意: solution: class Solution { public: bool checkRecord(str ...

  6. 【LeetCode】299. Bulls and Cows 解题报告(Python)

    [LeetCode]299. Bulls and Cows 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...

  7. 【LeetCode】743. Network Delay Time 解题报告(Python)

    [LeetCode]743. Network Delay Time 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: ht ...

  8. 【LeetCode】518. Coin Change 2 解题报告(Python)

    [LeetCode]518. Coin Change 2 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目 ...

  9. 【LeetCode】474. Ones and Zeroes 解题报告(Python)

    [LeetCode]474. Ones and Zeroes 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

随机推荐

  1. jQuery ajax常用示例

    总结一下jQuery ajax常用示例 $.ajax({ type: "post", //类型get,post url: urls, //链接地址 data:{"id&q ...

  2. 质量体系建设之路---可视化的MockServer

    一. 背景 福禄网络作为一家数字权益商品及服务提供商,覆盖了我们衣食住行的各种生活场景的权益内容,对接了如支付宝.京东.银行APP各种渠道,如何能够快速的响应渠道需求,提供稳定的接口服务,这就要求我们 ...

  3. JavaScript | 新手村(一)变量,运算和变量方法

    资料来自:JavaScript 第一步 1. 向 html 页面添加 JavaScript 1.1 内部 JavaScript 在 html 文件中的 </body> 标签前插入代码: & ...

  4. 『学了就忘』Linux文件系统管理 — 64、磁盘配额的配置步骤

    目录 1.手工建立一个5GB的分区 2.建立需要做限制的三个用户 3.在分区上开启磁盘配额功能 4.建立磁盘配额的配置文件 5.开始设置用户和组的配额限制 6.启动和关闭配额 7.磁盘配额的查询 8. ...

  5. A Child's History of England.28

    By such means, and by taxing and oppressing the English people in every possible way, the Red King b ...

  6. android Paint 详解

    /**     * Paint类介绍 * * Paint即画笔,在绘图过程中起到了极其重要的作用,画笔主要保存了颜色, * 样式等绘制信息,指定了如何绘制文本和图形,画笔对象有很多设置方法, * 大体 ...

  7. ORACEL 创建DIRECTORY

    oracle要直接对文件进行读写必须先创建一个DIRECTORY. 语法如下: CREATE DIRECTORY UTL_FILE_DIR AS '/home/oracle/oradir'; 可以通过 ...

  8. Output of C++ Program | Set 3

    Predict the output of below C++ programs. Question 1 1 #include<iostream> 2 using namespace st ...

  9. CentOS 初体验三: Yum 安装、卸载软件

    一:Yum 简介 Yum(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及CentOS中的Shell前端软件包管理器.基于RPM包管理,能够从指 ...

  10. String类型和包装类型作为参数传递时,是属于值传递还是引用传递呢?

    原理知识: 如果参数类型是原始类型,那么传过来的就是这个参数的一个副本,也就是这个原始参数的值,这个跟之前所谈的传值是一样的.如果在函数中改变了副本的 值不会改变原始的值. 如果参数类型是引用类型,那 ...