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

本文介绍了一种用于检查学生考勤记录是否符合奖励条件的简单算法。该算法需确保记录中不存在超过一次的缺席('A')或连续三次以上的迟到('L')。通过遍历字符串并计数特定字符实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

  1. 'A' : Absent.
  2. 'L' : Late.
  3. '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

 

 


 

题目标签:String

  题目让我们检查String s,不能有超过1个A 或者 不能连着有超过2个L。

  检查A 很容易,检查L 的话,要检查连续的,只要在不是L的情况下,把 count_L reset 回到2就可以了。具体请看code。

 

 

Java Solution:

Runtime beats 98.72% 

完成日期:04/21/2018

关键词:String

关键点:reset count_L if this char is not 'L'

 1 class Solution 
 2 {
 3     public boolean checkRecord(String s) 
 4     {
 5         char [] arr = s.toCharArray();
 6         int count_A = 1;
 7         int count_L = 2;       
 8 
 9         for(int i=0; i<arr.length; i++)
10         {
11             char c = arr[i];
12             
13             if(c == 'L') // if char is 'L'
14             {
15                 count_L--;
16             }   // if char is 'A' or 'P'
17             else
18             {
19                 if(c == 'A')
20                     count_A--;
21                 
22                 count_L = 2; // if this char is not L, set count_L to 2
23             }
24             
25             if(count_A < 0 || count_L < 0)
26                 return false;
27         }
28         
29         return true;
30     }
31 }

参考资料:n/a

LeetCode 题目列表 - LeetCode Questions List

题目来源:https://leetcode.com/

转载于:https://www.cnblogs.com/jimmycheng/p/8905442.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值