leetcode551: Student Attendance Record I

来源:互联网 发布:音频信号发生器软件 编辑:程序博客网 时间:2024/06/04 19:05

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
public boolean checkRecord(String s) {char[] c = s.toCharArray();int countA = 0;boolean countL1 = false;boolean countL2 = false;for (int i = 0; i < c.length; i++) {if (c[i] == 'A') {countA++;countL1 = false;countL2 = false;} else if (c[i] == 'P') {countL1 = false;countL2 = false;} else if (countL1 != true)countL1 = true;else if (countL1 = true && countL2 != true)countL2 = true;elsereturn false;if (countA > 1)return false;}return true;}



0 0
原创粉丝点击