作者: 负雪明烛
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 日 —— 美妙的周末,美丽的天气

最新文章

  1. 【转载】ANSYS 动力分析 (9) - 瞬态动力分析 (1)
  2. sobel算子
  3. 【CodeVS 3289】【NOIP 2013】花匠
  4. hdu 1698:Just a Hook(线段树,区间更新)
  5. Security » Authorization » 基于角色的授权
  6. July 31st, Week 32nd Sunday, 2016
  7. Java:网络编程之UDP的使用
  8. PHP集成支付宝快速实现充值功能
  9. Visual C++ 打印编程技术-编程基础-获取打印机
  10. typeahead使用配置参数。
  11. 从汇编角度来理解linux下多层函数调用堆栈执行状态
  12. SSM 使用方法
  13. jsp/servlet相关技术及知识
  14. Django中session的基础了解
  15. 使用python脚本实现iOS图片资源压缩
  16. JAVA基础知识总结:十八
  17. Ogre2.1 结合OpenGL3+高效渲染
  18. 别再犯低级错误,带你了解更新缓存的四种Desigh Pattern
  19. TSQL update 简单应用小总结
  20. YII cookie和session的使用方法

热门文章

  1. 34. Swap Nodes in Pairs
  2. CentOS6配置邮件发送
  3. Spring Boot 热启动插件
  4. 学习java的第十六天
  5. Spark(十三)【SparkSQL自定义UDF/UDAF函数】
  6. Angular 组件通信的三种方式
  7. 打破砂锅问到底!HTTP和HTTPS详解
  8. 【leetcode】797. All Paths From Source to Target
  9. 【Java 设计】如何优雅避免空指针调用
  10. 理解css中的 content:" " 是什么意思