【LeetCode】551. Student Attendance Record I 解题报告(Java & Python)
作者: 负雪明烛
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 日 —— 美妙的周末,美丽的天气
最新文章
- 【转载】ANSYS 动力分析 (9) - 瞬态动力分析 (1)
- sobel算子
- 【CodeVS 3289】【NOIP 2013】花匠
- hdu 1698:Just a Hook(线段树,区间更新)
- Security » Authorization » 基于角色的授权
- July 31st, Week 32nd Sunday, 2016
- Java:网络编程之UDP的使用
- PHP集成支付宝快速实现充值功能
- Visual C++ 打印编程技术-编程基础-获取打印机
- typeahead使用配置参数。
- 从汇编角度来理解linux下多层函数调用堆栈执行状态
- SSM 使用方法
- jsp/servlet相关技术及知识
- Django中session的基础了解
- 使用python脚本实现iOS图片资源压缩
- JAVA基础知识总结:十八
- Ogre2.1 结合OpenGL3+高效渲染
- 别再犯低级错误,带你了解更新缓存的四种Desigh Pattern
- TSQL update 简单应用小总结
- YII cookie和session的使用方法