Leetcode 551. Student Attendance Record I--判断是否奖励: A出现次数大于1返回false;L连续出现次数大于2返回false,中间隔断时从0开始计数
程序员文章站
2022-05-01 22:51:44
...
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返回false(可不连续) ;L连续出现次数大于2返回false,中间隔断时从0开始计数
package com.string;
/**
* Title:
* A出现次数大于1返回false(可不连续)
* L连续出现次数大于2返回false,中间隔断时从0开始计数
*
* @Author
* @CreateTime 2019/6/26 17:21
*/
public class Leetcode_551StudentAttendanceRecordI {
/**
* A出现次数大于1返回false(可不连续)
* L连续出现次数大于2返回false,中间隔断时从0开始计数
* Runtime: 0 ms, faster than 100.00% of Java online submissions for Student Attendance Record I.
* Memory Usage: 34.4 MB, less than 100.00% of Java online submissions for Student Attendance Record I.
*
* @param s
* @return
*/
public boolean checkRecord(String s) {
int aCount = 0;
int lCount = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'A') {
aCount++;
lCount = 0;
if (aCount > 1) {
return false;
}
} else if (s.charAt(i) == 'L') {
lCount++;
if (lCount > 2) {
return false;
}
} else {
lCount = 0;
}
}//for
return true;
}
public static void main(String[] args) {
Leetcode_551StudentAttendanceRecordI object = new Leetcode_551StudentAttendanceRecordI();
System.out.println(object.checkRecord("PPALLP"));//true
System.out.println(object.checkRecord("PPALLL"));//false
System.out.println(object.checkRecord("LALL"));//true
}
}
上一篇: js里arr[1]>arr[-1]结果为false
下一篇: 0与1和true与false的关系