【LeetCode】Student Attendance Record I 解题报告

来源:互联网 发布:怎么查看服务端口 编辑:程序博客网 时间:2024/05/16 11:21

【LeetCode】Student Attendance Record I 解题报告

标签(空格分隔): LeetCode


题目地址: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: TrueExample 2:Input: "PPALLL"Output: False

Ways

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

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

Date

2017 年 4 月 21 日

0 0