作者: 负雪明烛
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. 64-Unique Binary Search Trees

    96. Unique Binary Search Trees My Submissions Question Editorial Solution Total Accepted: 82788 Tota ...

  2. vim 的使用

    基本操作:  命令行模式 进入命令行 打开文本的时候,直接进去命令行模式 在其它模式按ESC,可以进入命令行模式 新建进入了命令行模式 光标进入末行"G"(shift+按键g,自学 ...

  3. LeetCode数组中重复的数字

    LeetCode 数组中重复的数字 题目描述 在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次. ...

  4. 链式栈——Java实现

    1 package struct; 2 3 //接口 4 interface ILinkStack{ 5 //栈中元素个数(栈大小) 6 int size(); 7 //取栈顶元素 8 Object ...

  5. JDK1.8新特性(一): 接口的默认方法default

    前言 今天在学习mysql分区优化时,发现一个博客专家大神,对其发布的文章简单学习一下: 一:简介 我们通常所说的接口的作用是用于定义一套标准.约束.规范等,接口中的方法只声明方法的签名,不提供相应的 ...

  6. 【Linux】【Services】【SaaS】Docker+kubernetes(12. 部署prometheus/grafana/Influxdb实现监控)

    1.简介 1.1. 官方网站: promethos:https://prometheus.io/ grafana:https://grafana.com/ 1.2. 架构图 2. 环境 2.1. 机器 ...

  7. 京东消息中间件JMQ(转)

    http://blog.csdn.net/javahongxi/article/details/54411464 [京东技术]京东的MQ经历了JQ->AMQ->JMQ的发展,其中JQ的基于 ...

  8. Git初始化及仓库创建和操作

    一.基本信息配置 1.全局配置用户名 git config --global user.name "YeHuan-byte" 2.全局配置邮箱 git config --globa ...

  9. 详解 java 异常

    Throwable 可以用来表示任何可以作为异常抛出的类(注意,是类不是接口),分为两种: Error(注意!error也是可以throw的,但是不建议) 和 Exception. 其中 Error ...

  10. Snort 入侵检测系统

    Snort 入侵检测系统 一.实验目的 1.掌握snort IDS工作原理 2.应用snort 三种方式工作 二.实验环境 系统环境:Windows环境, kali环境 三.实验原理 1.snort ...