LeetCode 551. Student Attendance Record I (学生出勤纪录 I)
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 containmore than one 'A' (absent)ormore 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'
class Solution { public boolean checkRecord(String s) { char [] arr = s.toCharArray(); int count_A = 1; int count_L = 2; for(int i=0; i<arr.length; i++) { char c = arr[i]; if(c == 'L') // if char is 'L' { count_L--; } // if char is 'A' or 'P' else { if(c == 'A') count_A--; count_L = 2; // if this char is not L, set count_L to 2 } if(count_A < 0 || count_L < 0) return false; } return true; } }
参考资料:n/a
LeetCode 题目列表 -LeetCode Questions List
题目来源:https://leetcode.com/
相关推荐
Lzs 2020-10-23
聚合室 2020-11-16
零 2020-09-18
Justhavefun 2020-10-22
jacktangj 2020-10-14
ChaITSimpleLove 2020-10-06
Andrea0 2020-09-18
周游列国之仕子 2020-09-15
afanti 2020-09-16
88234852 2020-09-15
YClimb 2020-09-15
风雨断肠人 2020-09-04
卖口粥湛蓝的天空 2020-09-15
stulen 2020-09-15
pythonxuexi 2020-09-06
abfdada 2020-08-26
梦的天空 2020-08-25